cocos2dx加载网络动态图片资源

前端之家收集整理的这篇文章主要介绍了cocos2dx加载网络动态图片资源前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

对于我刚开始学cocos2dx的初学者来说,连c++都不熟悉,写起来真是蛋疼,网上的说法千奇百怪,几乎都是过时的,没办法,只有一步一步自己学习了,先写篇文章丢下丑。

在网上下载了一份帮助文档,还是有点过时的,不过不打紧,大部分还是有效的。到时我也上传一份。

首先,头文件的设置

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "network//HttpRequest.h"  //要引入
#include "network//HttpClient.h"   //要引入

#include "cocos2d.h"
using namespace cocos2d::network;//使用命名空间,听同事说using namespace cocos2d::network  
//和#include "network/.." 类似于java的导包
class HelloWorld : public cocos2d::Layer{
public: 
	static cocos2d::Scene* createScene(); 
	virtual bool init(); 
	void onGetFinished(Node* node,void * obj);//回调函数,这里有参数Node和void Finish(void* obj);
	//对调函数,只有void 这里的Finish函数在这里只是做对比,实际是不要的。 
	CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__


 
 
 .cpp文件 
 
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
	// 'scene' is an autorelease object
	auto scene = Scene::create();

	// 'layer' is an autorelease object
	auto layer = HelloWorld::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
	//////////////////////////////
	// 1. super init first
	if (!Layer::init())
	{
		return false;
	}
	auto visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	std::string path = FileUtils::getInstance()->getWritablePath() + "ceshi.png";
	if (FileUtils::getInstance()->isFileExist(path)) //本地有就走本地,没有就走网络
	{
		Sprite* sprite = Sprite::create(path);
		sprite->setPosition(Vec2(50,50));
		this->addChild(sprite);
	}
	else{

		HttpClient* httpClient = HttpClient::getInstance();

		HttpRequest* httprequest = new HttpRequest();
		httprequest->setRequestType(HttpRequest::Type::GET);
		httprequest->setUrl("http://avatar.csdn.net/A/6/5/1_qqxj2012.jpg");
		httprequest->setResponseCallback(this,callfuncND_selector(HelloWorld::onGetFinished));
		//httprequest->setResponseCallback(this,CC_CALLBACK_1(HelloWorld::Finish,this));这里是对比,这里不支持CC_CALLBACK此类的函数回调
		//支持类似callfuncND_selector的普通回调,callfuncND_selector    typedef void (Ref::*SEL_CallFuncND)(Node*,void*);,所以头文件的回调函数
		//是Node 和void
		httprequest->setTag("PicGet");
		httpClient->setTimeoutForConnect(30);
		httpClient->send(httprequest);
		httprequest->release();//用完之后释放
	}
	return true;
}
//对比的
void HelloWorld::Finish(void* data){
	HttpResponse* response = (HttpResponse*)data;
	if (!response){
		return;
	}
	if (!response->isSucceed()){
		CCLOG("response Failed");
		CCLOG("error buffer: %s",response->getErrorBuffer()); 
		return;
	}
	if (0 != strlen(response->getHttpRequest()->getTag())){ 
		CCLOG("%s completed",response->getHttpRequest()->getTag()); 
	}
	std::vector<char> *buffData = response->getResponseData(); 
	CCImage* img = new CCImage;
	img->initWithImageData((unsigned char*)buffData->data(),buffData->size()); 
	CCTexture2D* texture = new CCTexture2D(); 
	bool isImg = texture->initWithImage(img); img->release(); 
	auto visibleSize = Director::getInstance()->getVisibleSize(); 
	Vec2 origin = Director::getInstance()->getVisibleOrigin(); 
	CCSprite* sprite = CCSprite::createWithTexture(texture); 
	sprite->setPosition(Vec2(visibleSize.width / 2,visibleSize.height / 2));
	this->addChild(sprite);
	char* buffer = (char*)malloc(buffData->size()); 
	std::copy(buffData->begin(),buffData->end(),buffer); 
	log(StringUtils::format("路径:%s",FileUtils::getInstance()->getWritablePath().c_str()).c_str()); 
	auto filename = FileUtils::getInstance()->getWritablePath() + "ceshi.png"; 
	FILE *fp = fopen(filename.c_str(),"wb+"); 
	fwrite(buffer,1,buffData->size(),fp); 
	fclose(fp);
}
void HelloWorld::onGetFinished(Node* node,void* data){ 
	HttpResponse* response = (HttpResponse*)data;
	if (!response){ 
		return;
	}
	if (!response->isSucceed()){
		CCLOG("response Failed"); 
		CCLOG("error buffer: %s",response->getErrorBuffer()); 
		return; 
	}
	if (0 != strlen(response->getHttpRequest()->getTag())){ 
		CCLOG("%s completed",response->getHttpRequest()->getTag()); 
	}
	std::vector<char> *buffData = response->getResponseData(); 
	CCImage* img = new CCImage; 
	img->initWithImageData((unsigned char*)buffData->data(),buffData->size());
	CCTexture2D* texture = new CCTexture2D(); bool isImg = texture->initWithImage(img); 
	img->release(); 
	auto visibleSize = Director::getInstance()->getVisibleSize(); 
	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	CCSprite* sprite = CCSprite::createWithTexture(texture); 
	sprite->setPosition(Vec2(visibleSize.width / 2,visibleSize.height / 2)); 
	this->addChild(sprite); 
	char* buffer = (char*)malloc(buffData->size()); 
	std::copy(buffData->begin(),buffer);
	log(StringUtils::format("路径:%s","wb+");
	fwrite(buffer,fp); 
	fclose(fp);
}


 
 
 

因为我已经下载了,所以显示在坐标(250,250),若是刚下载的应该是在可视区域的中间。

要在android上跑记得在清单文件加上权限

<!-- 访问internet权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REORDER_TASKS"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>  
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 

哈哈,个人还是比较菜的,写的不好多见谅啊。

原文链接:https://www.f2er.com/cocos2dx/338992.html

猜你在找的Cocos2d-x相关文章