有时候场景中的资源加载过多的话就会引起游戏进入的时候很卡,因为那是边加载边显示。还有在游戏中按下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然后在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));然后在回调函数
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);