Error[8]: Undefined offset: 3, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述Cocos2d-x 2.1.4 里面已经为我们提供了HttpRequest ,HttpClient,CCHttpResponse用于处理网络相关 *** 作 下面简单利用这些封装好的类,来读取网络图片创建精灵 1.在工程里面导入HttpRequest.h,HttpClient.h #include "HelloWorldScene.h"#include "SimpleAudioEngine.h"#i

Cocos2d-x 2.1.4 里面已经为我们提供了httpRequest ,httpClIEnt,CChttpResponse用于处理网络相关 *** 作

下面简单利用这些封装好的类,来读取网络图片并创建精灵

1.在工程里面导入httpRequest.h,httpClIEnt.h

#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"#include "httpRequest.h"#include "httpClIEnt.h"USING_NS_CC;USING_NS_CC_EXT;using namespace std;CCScene* HelloWorld::scene(){    CCScene *scene = CCScene::create();    HelloWorld *layer = HelloWorld::create();    scene->addChild(layer);    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    if ( !cclayer::init() )    {        return false;    }    CcmenuItemImage *pCloseItem = CcmenuItemImage::create(                                        "Closenormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback) );    pCloseItem->setposition( ccp(CCDirector::sharedDirector()->getWinSize().wIDth - 20,20) );    Ccmenu* pMenu = Ccmenu::create(pCloseItem,NulL);    pMenu->setposition( CCPointZero );    this->addChild(pMenu,1);//  这是个静态方法,得到一个httpClIEnt实例    CChttpClIEnt* httpClIEnt = CChttpClIEnt::getInstance();//    创建一个CChttpRequest对象    CChttpRequest* httpReq =new CChttpRequest();//    设置请求类型    httpReq->setRequestType(CChttpRequest::khttpGet);//  设置请求Url(可以更具需要从Json 或xml,甚至HTML里解析获得到这个图片url)    httpReq->setUrl("http://avatar.csdn.net/A/6/5/1_qqxj2012.jpg");//    请求完成后回调    httpReq->setResponseCallback(this,callfuncND_selector(HelloWorld::onGetFinished));//   为请求设置标签,后面可以根据这个标签来获取我们要的数据    httpReq->setTag("PicGet");//    设置连接超时时间    httpClIEnt->setTimeoutForConnect(30);    httpClIEnt->send(httpReq);    httpReq->release();    httpReq=NulL;    return true;}voID HelloWorld::onGetFinished(CCNode* node,CCObject* obj){    CChttpResponse* response = (CChttpResponse*)obj;//    判断是否响应成功    if (!response->isSucceed())    {        cclog("Receive Error! %s\n",response->getErrorBuffer());        return ;    }        const char* tag = response->gethttpRequest()->getTag();    if (0 == strcmp("PicGet",tag))    {        vector<char> *data = response->getResponseData();        int data_length = data->size();        string res;        for (int i = 0;i<data_length;++i)        {            res+=(*data)[i];        }        res+='1.AndroID 运行前,记得在AndroIDManifest.xml 开启网络,读取SD卡等权限,不然不会显示!';        cclog("%s",res.c_str());    }        // 数据转存    unsigned char* pBuffer = NulL;    unsigned long bufferSize = 0;    vector<char> *buffer = response->getResponseData();    string path = CCfileUtils::sharedfileUtils()->getWritablePath()+"test.png";    pBuffer = CCfileUtils::sharedfileUtils()->getfileData(path.c_str(),"r",&bufferSize);    string buff(buffer->begin(),buffer->end());        //保存到本地文件    cclOG("path: %s",path.c_str());    file *fp = fopen(path.c_str(),"wb+");    fwrite(buff.c_str(),1,buffer->size(),fp);    fclose(fp);    }voID HelloWorld::menuCloseCallback(CCObject* pSender){    unsigned char* pBuffer = NulL;    unsigned long bufferSize = 0;    string path = CCfileUtils::sharedfileUtils()->getWritablePath()+"test.png";    pBuffer = CCfileUtils::sharedfileUtils()->getfileData(path.c_str(),&bufferSize);    if (!pBuffer)    {        return ;    }    CCImage* img = new CCImage;    img->initWithImageData(pBuffer,bufferSize);    free(pBuffer);    CCTexture2D* texture = new cocos2d::CCTexture2D();    bool isimg = texture->initWithImage(img);    img->release();    if (!isimg)    {        delete texture;        return ;    }    //这里建议使用成员变量来保存精灵,不然有可能导致显示白色块,出现异常!    sp =CCSprite::createWithTexture(texture);    CCSprite* sprite = CCSprite::createWithTexture(sp->getTexture());    sprite->setposition(ccp(240,160));    this->addChild(sprite,10);    texture->release();    }

IOS运行截图:

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> 

[+++]

2.这里只是简单实现,并没有实现图片异步加载,后续逐渐完善!




FROM:http://www.xuebuyuan.com/1353683.HTML

总结

以上是内存溢出为你收集整理的Cocos2d-x 读取网络图片创建精灵全部内容,希望文章能够帮你解决Cocos2d-x 读取网络图片创建精灵所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Cocos2d-x 读取网络图片创建精灵_app_内存溢出

Cocos2d-x 读取网络图片创建精灵

Cocos2d-x 读取网络图片创建精灵,第1张

概述Cocos2d-x 2.1.4 里面已经为我们提供了HttpRequest ,HttpClient,CCHttpResponse用于处理网络相关 *** 作 下面简单利用这些封装好的类,来读取网络图片创建精灵 1.在工程里面导入HttpRequest.h,HttpClient.h #include "HelloWorldScene.h"#include "SimpleAudioEngine.h"#i

Cocos2d-x 2.1.4 里面已经为我们提供了httpRequest ,httpClIEnt,CChttpResponse用于处理网络相关 *** 作

下面简单利用这些封装好的类,来读取网络图片并创建精灵

1.在工程里面导入httpRequest.h,httpClIEnt.h

#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"#include "httpRequest.h"#include "httpClIEnt.h"USING_NS_CC;USING_NS_CC_EXT;using namespace std;CCScene* HelloWorld::scene(){    CCScene *scene = CCScene::create();    HelloWorld *layer = HelloWorld::create();    scene->addChild(layer);    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    if ( !cclayer::init() )    {        return false;    }    CcmenuItemImage *pCloseItem = CcmenuItemImage::create(                                        "Closenormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback) );    pCloseItem->setposition( ccp(CCDirector::sharedDirector()->getWinSize().wIDth - 20,20) );    Ccmenu* pMenu = Ccmenu::create(pCloseItem,NulL);    pMenu->setposition( CCPointZero );    this->addChild(pMenu,1);//  这是个静态方法,得到一个httpClIEnt实例    CChttpClIEnt* httpClIEnt = CChttpClIEnt::getInstance();//    创建一个CChttpRequest对象    CChttpRequest* httpReq =new CChttpRequest();//    设置请求类型    httpReq->setRequestType(CChttpRequest::khttpGet);//  设置请求Url(可以更具需要从Json 或xml,甚至HTML里解析获得到这个图片url)    httpReq->setUrl("http://avatar.csdn.net/A/6/5/1_qqxj2012.jpg");//    请求完成后回调    httpReq->setResponseCallback(this,callfuncND_selector(HelloWorld::onGetFinished));//   为请求设置标签,后面可以根据这个标签来获取我们要的数据    httpReq->setTag("PicGet");//    设置连接超时时间    httpClIEnt->setTimeoutForConnect(30);    httpClIEnt->send(httpReq);    httpReq->release();    httpReq=NulL;    return true;}voID HelloWorld::onGetFinished(CCNode* node,CCObject* obj){    CChttpResponse* response = (CChttpResponse*)obj;//    判断是否响应成功    if (!response->isSucceed())    {        cclog("Receive Error! %s\n",response->getErrorBuffer());        return ;    }        const char* tag = response->gethttpRequest()->getTag();    if (0 == strcmp("PicGet",tag))    {        vector<char> *data = response->getResponseData();        int data_length = data->size();        string res;        for (int i = 0;i<data_length;++i)        {            res+=(*data)[i];        }        res+='1.AndroID 运行前,记得在AndroIDManifest.xml 开启网络,读取SD卡等权限,不然不会显示!';        cclog("%s",res.c_str());    }        // 数据转存    unsigned char* pBuffer = NulL;    unsigned long bufferSize = 0;    vector<char> *buffer = response->getResponseData();    string path = CCfileUtils::sharedfileUtils()->getWritablePath()+"test.png";    pBuffer = CCfileUtils::sharedfileUtils()->getfileData(path.c_str(),"r",&bufferSize);    string buff(buffer->begin(),buffer->end());        //保存到本地文件    cclOG("path: %s",path.c_str());    file *fp = fopen(path.c_str(),"wb+");    fwrite(buff.c_str(),1,buffer->size(),fp);    fclose(fp);    }voID HelloWorld::menuCloseCallback(CCObject* pSender){    unsigned char* pBuffer = NulL;    unsigned long bufferSize = 0;    string path = CCfileUtils::sharedfileUtils()->getWritablePath()+"test.png";    pBuffer = CCfileUtils::sharedfileUtils()->getfileData(path.c_str(),&bufferSize);    if (!pBuffer)    {        return ;    }    CCImage* img = new CCImage;    img->initWithImageData(pBuffer,bufferSize);    free(pBuffer);    CCTexture2D* texture = new cocos2d::CCTexture2D();    bool isimg = texture->initWithImage(img);    img->release();    if (!isimg)    {        delete texture;        return ;    }    //这里建议使用成员变量来保存精灵,不然有可能导致显示白色块,出现异常!    sp =CCSprite::createWithTexture(texture);    CCSprite* sprite = CCSprite::createWithTexture(sp->getTexture());    sprite->setposition(ccp(240,160));    this->addChild(sprite,10);    texture->release();    }

IOS运行截图:

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> 

2.这里只是简单实现,并没有实现图片异步加载,后续逐渐完善!




FROM:http://www.xuebuyuan.com/1353683.HTML

总结

以上是内存溢出为你收集整理的Cocos2d-x 读取网络图片创建精灵全部内容,希望文章能够帮你解决Cocos2d-x 读取网络图片创建精灵所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存