cocos2d-x 关于home键黑屏和游戏loading界面
来源:互联网 发布:淘宝达人管理 编辑:程序博客网 时间:2024/04/19 09:01
转自:http://blog.csdn.net/qq634416025/article/details/8041363
有时候场景中的资源加载过多的话就会引起游戏进入的时候很卡,因为那是边加载边显示。还有在游戏中按下home键 再进入游戏的时候会有一段时间的黑屏,那是因为游戏要重新加载资源,这样用户体验非常不好。
我在tests例子里面发现一个很好的例子叫做TextureCacheTest,里面讲解了如何写loading。
首先在类头文件里面写上:
- //loading......callback
- void loadingCallBack(cocos2d::CCObject *obj);
- void addSprite();
- cocos2d::CCLabelTTF *m_pLabelLoading;
- cocos2d::CCLabelTTF *m_pLabelPercent;
- int m_nNumberOfSprites;//计数用 在构造函数中初始化成要加载的资源个数
- int m_nNumberOfLoadedSprites;//初始化为0
- //loading......callback
- void loadingCallBack(cocos2d::CCObject *obj);
- void addSprite();
- cocos2d::CCLabelTTF *m_pLabelLoading;
- cocos2d::CCLabelTTF *m_pLabelPercent;
- int m_nNumberOfSprites;//计数用 在构造函数中初始化成要加载的资源个数
- int m_nNumberOfLoadedSprites;//初始化为0
然后在cpp里面实现:这是我随便截取的代码
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- //////////////////////////////////////////////////////////////////////////
- //loading界面
- m_pLabelLoading = CCLabelTTF::create("loading...", "Arial", 15);
- m_pLabelPercent = CCLabelTTF::create("%0", "Arial", 15);
- m_pLabelLoading->setPosition(CCPointMake(size.width / 2, size.height / 2 - 20));
- m_pLabelPercent->setPosition(CCPointMake(size.width / 2, size.height / 2 + 20));
- this->addChild(m_pLabelLoading,10);
- this->addChild(m_pLabelPercent,10);
- //////////////////////////////////////////////////////////////////////////
- CCTextureCache::sharedTextureCache()->addImageAsync("gobacknormal.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("gobackselected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/achievementnormal.png",this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/achievementselected.png",this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle1normal.png",this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle2normal.png",this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle3normal.png",this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle1selected.png",this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle2selected.png",this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle3selected.png",this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle1lock.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle2lock.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle3lock.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle2lockselected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle3lockselected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/main.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Smallhurdle/starfull.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- //////////////////////////////////////////////////////////////////////////
- //loading界面
- m_pLabelLoading = CCLabelTTF::create("loading...", "Arial", 15);
- m_pLabelPercent = CCLabelTTF::create("%0", "Arial", 15);
- m_pLabelLoading->setPosition(CCPointMake(size.width / 2, size.height / 2 - 20));
- m_pLabelPercent->setPosition(CCPointMake(size.width / 2, size.height / 2 + 20));
- this->addChild(m_pLabelLoading,10);
- this->addChild(m_pLabelPercent,10);
- //////////////////////////////////////////////////////////////////////////
- CCTextureCache::sharedTextureCache()->addImageAsync("gobacknormal.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("gobackselected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/achievementnormal.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/achievementselected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle1normal.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle2normal.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle3normal.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle1selected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle2selected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle3selected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle1lock.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle2lock.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle3lock.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle2lockselected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/Bighurdle3lockselected.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Bighurdle/main.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("Smallhurdle/starfull.png", this, callfuncO_selector(BighurdleLayer::loadingCallBack));
然后在回调函数
- void BighurdleLayer::loadingCallBack(cocos2d::CCObject *obj)
- {
- ++m_nNumberOfLoadedSprites;
- char tmp[10];
- sprintf(tmp,"%%%d", (int)(((float)m_nNumberOfLoadedSprites / m_nNumberOfSprites) * 100));
- m_pLabelPercent->setString(tmp);
- if (m_nNumberOfLoadedSprites == m_nNumberOfSprites)
- {
- this->removeChild(m_pLabelLoading, true);
- this->removeChild(m_pLabelPercent, true);
- addSprite();//这里是自己写得添加精灵的函数
- }
- }
- void BighurdleLayer::loadingCallBack(cocos2d::CCObject *obj)
- {
- ++m_nNumberOfLoadedSprites;
- char tmp[10];
- sprintf(tmp,"%%%d", (int)(((float)m_nNumberOfLoadedSprites / m_nNumberOfSprites) * 100));
- m_pLabelPercent->setString(tmp);
- if (m_nNumberOfLoadedSprites == m_nNumberOfSprites)
- {
- this->removeChild(m_pLabelLoading, true);
- this->removeChild(m_pLabelPercent, true);
- addSprite();//这里是自己写得添加精灵的函数
- }
- }
如果有更好的方法,请告知我,谢谢 共同进步
二.当按home键到桌面去,然后回到游戏弹出个进度条 然后游戏开始
我们只需要在AppDelegate.cpp里面添加layer就可以了 然后layer里面弄上定时器 一段时间后自动取消
- // this function will be called when the app is active again
- void AppDelegate::applicationWillEnterForeground()
- {
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- Loading *loading = new Loading();
- //CCSprite *sp = CCSprite::create("HelloWorld.png");
- //sp->setPosition(ccp(winSize.width/2, winSize.height/2));
- //loading->addChild(sp);
- CCDirector::sharedDirector()->getRunningScene()->addChild((CCNode *)loading);
- // this function will be called when the app is active again
- void AppDelegate::applicationWillEnterForeground()
- {
- CCSize winSize = CCDirector::sharedDirector()->getWinSize();
- Loading *loading = new Loading();
- //CCSprite *sp = CCSprite::create("HelloWorld.png");
- //sp->setPosition(ccp(winSize.width/2, winSize.height/2));
- //loading->addChild(sp);
- CCDirector::sharedDirector()->getRunningScene()->addChild((CCNode *)loading);
- cocos2d-x关于home键黑屏和游戏loading界面
- cocos2d-x 关于home键黑屏和游戏loading界面
- cocos2d-x 关于home键黑屏和游戏loading界面
- cocos2d-x 实现按Home和待机键后,再进入游戏显示暂停界面
- cocos2d-x 实现按Home和待机键后,再进入游戏显示暂停界面
- cocos2d-x游戏开发(十五)游戏加载动画loading界面
- cocos2d-x游戏开发(十五)游戏加载动画loading界面
- cocos2d-x游戏开发(十五)游戏加载动画loading界面
- Cocos2d-x 3.2 游戏资源Loading界面简单Demo
- cocos2d-x(quick--cocos2d-x)引擎开发的游戏在android平台上,锁屏,按HOME键,重反游戏后黑屏问题
- Cocos2d-x 3.x 异步加载资源和Loading界面
- cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
- cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
- cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
- cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
- cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
- cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
- cocos2d-x Android游戏黑屏解决办法
- cocos2d-x 中文支持问题
- 重载
- cocos2d-x 数学函数、常用宏粗整理 - by Glede
- mysql判断某字段是否为空
- Android中如何查找内存泄露
- cocos2d-x 关于home键黑屏和游戏loading界面
- cocos2d-x android平台切入后台纹理失效的解决方法
- VC中使用richtextbox使用
- 海量小文件存储
- cocos2d-x 触屏事件
- Redis命令总结
- cocos2d-x 播放音乐和音效
- DataGridView删除多行
- 无参函数的调用