How to make a sprite from URL in cocos2d-x
2015-06-28 23:42
1776 查看
Hello friends. In this tutorial I am going to explain how you will be making a sprite from any URL on Internet
I am using two URLs
1. http://lptpl.info/9.png
2. https://dl.dropboxusercontent.com/u/37517735/icon_test.jpg
So these are the two images I will be using to create the sprite
Step 1:
Step 1 is as simple. Just create a new project in cocos2d-x
Step 2:
Now just remove the extra matter in your HelloWorldScene.h so that it looks like
Step 3:
Now in your HelloWorldScene.h, add the following headers and namespaces
HttpClient.h is used for the request and response of the URL
Step 4:
Now we add some labels and functions such that the class looks like
Step 5:
In your HelloWorldScene.cpp class add the following after the after the inclusion of HelloWorldScene.h class
These two URLs will be used for fetching the images
Step 6:
Now in your init function just add the label and a button. On button click I will be loading the URL and will be making it a sprite
So now your init function looks like
Step 7:
On button click the downLoadImage() function is called
The definition of downLoadImage() function is as follows
Here I have set the name as img.png. This means that the image downloaded will be saved as img.png in your simulators library folder. I will show you the path later on in the next steps. Then the responsecallback is set to call another function onImageDownLoaded()
The definition of onImageDownLoaded() is as follows
Your image will be stored in somewhat path like this
And now you are done. Now run the project
Output will be
Now change the URL and you can see other image
Hope it helped you and you liked it.
I am using two URLs
1. http://lptpl.info/9.png
2. https://dl.dropboxusercontent.com/u/37517735/icon_test.jpg
So these are the two images I will be using to create the sprite
Step 1:
Step 1 is as simple. Just create a new project in cocos2d-x
Step 2:
Now just remove the extra matter in your HelloWorldScene.h so that it looks like
12345678 | bool HelloWorld::init(){ if ( !CCLayer::init() ) { return false; }return true;} |
Now in your HelloWorldScene.h, add the following headers and namespaces
1 2 3 | #include "HttpClient.h" using namespace cocos2d; using namespace cocos2d::extension; |
Step 4:
Now we add some labels and functions such that the class looks like
123456789101112 | class HelloWorld : public cocos2d::CCLayer{ CCLabelTTF* download;public: virtual bool init(); static cocos2d::CCScene* scene(); void downloadButton_click(CCObject* pSender); void downLoadImage(); void onImageDownLoaded(CCHttpClient* pSender,CCHttpResponse* pResponse); CREATE_FUNC(HelloWorld);}; |
In your HelloWorldScene.cpp class add the following after the after the inclusion of HelloWorldScene.h class
1 2 3 4 | #define URL "http://lptpl.info/9.png" #define anotherURL "https://dl.dropboxusercontent.com/u/37517735/icon_test.jpg" using namespace cocos2d::extension; USING_NS_CC; |
Step 6:
Now in your init function just add the label and a button. On button click I will be loading the URL and will be making it a sprite
So now your init function looks like
12345678910111213141516171819202122232425 | bool HelloWorld::init(){ if ( !CCLayer::init() ) { return false; } CCSize winSize=CCDirector::sharedDirector()->getWinSize(); CCMenuItemImage *downLoadButton = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::downloadButton_click)); downLoadButton ->setPosition(ccp(winSize.width/2,winSize.height*.10)); download=CCLabelTTF::create("", "Arial", 30); download->setPosition(ccp(winSize.width/2,winSize.height/2)); this->addChild(download); CCMenu* pMenu = CCMenu::create(downLoadButton,NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); return true;} |
On button click the downLoadImage() function is called
1 2 3 4 | void HelloWorld::downloadButton_click(CCObject* pSender) { downLoadImage(); } |
12345678910111213 | void HelloWorld::downLoadImage(){ download->setString("downloading....."); std::string strImage = "img.png"; CCHttpRequest* request = new CCHttpRequest(); request->setUrl(anotherURL); //request->setUrl(URL); request->setRequestType(CCHttpRequest::kHttpGet); request->setResponseCallback(this, httpresponse_selector(HelloWorld::onImageDownLoaded)); request->setTag(strImage.c_str()); CCHttpClient::getInstance()->send(request); request->release();} |
The definition of onImageDownLoaded() is as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | void HelloWorld::onImageDownLoaded(CCHttpClient* pSender, CCHttpResponse* pResponse) { CCSize winSize=CCDirector::sharedDirector()->getWinSize(); CCHttpResponse* response = pResponse; if (!response) { CCLog("No Response"); return; } int statusCode = response->getResponseCode(); char statusString[64] = {}; sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); CCLog("response code: %d", statusCode); if (!response->isSucceed()) { CCLog("response failed"); CCLog("error buffer: %s", response->getErrorBuffer()); CCMessageBox("error in downloaded", "Image"); return; } std::vector<char>*buffer = response->getResponseData(); CCImage * img=new CCImage(); img->initWithImageData(&(buffer->front()), buffer->size()); CCMessageBox("Image downloaded", "Image"); // Save image file to device. std::string writablePath = CCFileUtils::sharedFileUtils()->getWritablePath(); writablePath.append(response->getHttpRequest()->getTag()); img->saveToFile(writablePath.c_str()); //Now create Sprite from downloaded image CCSprite* pSprite = CCSprite::create(writablePath.c_str()); pSprite->setPosition(ccp(winSize.width/2,winSize.height/2)); addChild(pSprite); } |
And now you are done. Now run the project
Output will be
Now change the URL and you can see other image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void HelloWorld::downLoadImage() { download->setString("downloading....."); std::string strImage = "img.png"; CCHttpRequest* request = new CCHttpRequest(); //request->setUrl(anotherURL); request->setUrl(URL); request->setRequestType(CCHttpRequest::kHttpGet); request->setResponseCallback(this, httpresponse_selector(HelloWorld::onImageDownLoaded)); request->setTag(strImage.c_str()); CCHttpClient::getInstance()->send(request); request->release(); } |
Hope it helped you and you liked it.
相关文章推荐
- Cocos2d-x的学习之旅(八)ScrollView
- cocos2d-x3.6 连连看触摸事件传递
- cocos2d-android设置透明背景
- 三、PageView Button的事件处理
- Cocos2d-x项目总结中的一些遇到的问题
- cocos-js动态读取json
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十课:Cocos2D-X场景切换1-3
- 2015.6.28开始写博客记录cocos2dx学习历程
- Cocos2d-x3.0 Json解析
- cocos2dx3.6 实现带光标的输入框
- Cocos2d-x制作跳棋第一步: 素材的准备
- cocos2d-js中的一些语法问题
- cocos2dx 场景间正向反向传
- 非常好的ubuntu搭建cocos2d环境
- cocos2d-x Lua引擎安装步骤
- 【Cocos游戏实战】功夫小子第五课之帮助场景和选关功能的实现
- cocos2dx基础篇(23)——进度条CCProgressTimer
- cocos2d-x3.6 连连看连通画线
- 【Cocos2d-x 3.2】裁剪节点(ClippingNode)总结
- Cocos2d-x在Android在竖屏切换