cocos2d-x Loading界面实现资源加载

本文介绍了在cocos2d-x游戏开发中,为避免因资源加载过多导致的游戏启动卡顿问题,可以采用预加载策略。通过参考TextureCacheTest示例,展示了如何创建一个Loading界面来实现资源的后台加载,确保游戏流畅进入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候场景中的资源加载过多的话就会引起游戏进入的时候很卡,因为那是边加载边显示。在tests例子里面有一个很好的例子叫做TextureCacheTest,里面讲解了如何写loading。

#include "LoadingScene.h"
#include "HelloWorldScene.h"
bool LoadingScene::init()
{
    totalNum=9; //记录总的加载数量
    haveLoadedNum=0;   //记录已加载的数量
    this->loading();
    return true;
}
CCScene *LoadingScene::scene()
{
    CCScene *scene=CCScene::create();
    LoadingScene *layer=LoadingScene::create();
    scene->addChild(layer);
    return scene;
}
void LoadingScene::loading()
{
    CCSize size=CCDirector::sharedDirector()->getWinSize();
    ttf=CCLabelTTF::create("%0", "Arial", 12);    //显示加载进度
    
    CCLabelTTF *havettf=CCLabelTTF::create("Loading", "Arial", 12);
    this->addChild(ttf,1);
    this->addChild(havettf,1);
    ttf->setPosition(ccp(size.width/3, size.height/2));
    havettf->setPosition(ccp(size.width/2, size.height/2));
    
    CCTextureCache::sharedTextureCache()->addImageAsync("youlost.png", this, callfuncO_selector(LoadingScene::loadedCallBack));
    CCTextureCache::sharedTextureCache()->addImageAsync("youwin.png", this, callfuncO_selector(LoadingScene::loadedCallBack));
    CCTextureCache::sharedTextureCache()->addImageAsync("cat.png", this, callfuncO_selector(LoadingScene::loadedCallBack));
    CCTextureCache::sharedTextureCache()->addImageAsync("catBody1.png", this, callfuncO_selector(LoadingScene::loadedCallBack));
    CCTextureCache::sharedTextureCache()->addImageAsync("catBody2-4.png", this, callfuncO_selector(LoadingScene::loadedCallBack));
    CCTextureCache::sharedTextureCache()->addImageAsync("catBody3.png", this, callfuncO_selector(LoadingScene::loadedCallBack));
    CCTextureCache::sharedTextureCache()->addImageAsync("catHand1.png", this, callfuncO_selector(LoadingScene::loadedCallBack));
    CCTextureCache::sharedTextureCache()->addImageAsync("catHand2.png", this, callfuncO_selector(LoadingScene::loadedCallBack));
    CCTextureCache::sharedTextureCache()->addImageAsync("catTail.png", this, callfuncO_selector(LoadingScene::loadedCallBack));  
}
void LoadingScene::loadedCallBack()
{
    haveLoadedNum++;
    this->runAction(CCDelayTime::create(15));
    char tmp[10];
    sprintf(tmp, "%%%d",(int)((float)haveLoadedNum/totalNum*100));
    ttf->setString(tmp);  //更改加载进度
    if (haveLoadedNum==9)
    {
        this->removeChild(ttf, true);   //加载完成后,移除加载进度显示
        CCScene *newscne=HelloWorld::scene();
        CCDirector::sharedDirector()->replaceScene(newscne); //场景切换
    }
}
这样,在HelloWorld中,就可以通过

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }   
    CCSprite *sp=CCSprite::createWithTexture(CCTextureCache::sharedTextureCache()->textureForKey("youlost.png"));
    addChild(sp,1);
	}
来获得预加载的图片,从而缓解游戏初步加载时的卡现象。



欢迎转载,转载请注明出处:http://blog.csdn.net/somestill/article/details/10258519

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值