转自:http://blog.csdn.net/zlqqhs/article/details/9269245
原理,使用
CCTextureCache::sharedTextureCache()->addImageAsync("background.png", this, callfuncO_selector(Assets::loadingCallBack));
加载纹理时,每加载一张纹理调用一次Assets::loadingCallBack,Assets::loadingCallBack实现进度条的进度跟进和判断跳转
步骤:
1.新建一个Cocos2d-x工程,保证能成功运行(我一直在说废话,呵呵)
2.在HelloWorldScene.h文件中修改和增加代码
3.在HelloWorldScene.cpp文件中修改和增加代码
4.新建SecondScene.h和SecondScene.cpp两个文件,用于加载完成后跳转
1.新建一个Cocos2d-x工程,保证能成功运行,没有其他的了
2.在HelloWorldScene.h文件中修改和增加代码
- HelloWorld();
- virtual bool init();
- static cocos2d::CCScene* scene();
- CREATE_FUNC(Assets);
- void loadingCallBack(cocos2d::CCObject *obj);//回调函数,每加载一张纹理就调用一次
- private:
- cocos2d::CCLabelTTF *m_pLabelLoading;//文字标签,用于显示“Loading”字样
- cocos2d::CCLabelTTF *m_pLabelPercent;//文字标签,用于显示百分比
- int currentNum;//当前加载到第几张
- int totalNum;//加载总数
HelloWorld();
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(Assets);
void loadingCallBack(cocos2d::CCObject *obj);//回调函数,每加载一张纹理就调用一次
private:
cocos2d::CCLabelTTF *m_pLabelLoading;//文字标签,用于显示“Loading”字样
cocos2d::CCLabelTTF *m_pLabelPercent;//文字标签,用于显示百分比
int currentNum;//当前加载到第几张
int totalNum;//加载总数
3.在HelloWorldScene.cpp文件中修改和增加代码
构造函数,初始化currentNum和totalNum
- HelloWorld::HelloWorld():currentNum(0),totalNum(500)
- {
- }
HelloWorld::HelloWorld():currentNum(0),totalNum(500)
{
}
修改init()中的do代码块为:
- CC_BREAK_IF(! CCLayer::init());
- //取得屏幕大小
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- //文字标签,用于显示Loding字样和加载的进度
- m_pLabelLoading = CCLabelTTF::create("Loading...", "Arial", 25);
- m_pLabelPercent = CCLabelTTF::create("0%", "Arial", 25) ;
- m_pLabelLoading->setPosition(ccp(size.width / 2, size.height / 2 + 50));
- m_pLabelPercent->setPosition(ccp(size.width / 2, size.height / 2));
- this->addChild(m_pLabelLoading);
- this->addChild(m_pLabelPercent);
- //加载HelloWorld.png纹理
- for (int i = 0; i < 500; i++)
- {
- //加载纹理,回调Assets::loadingCallBack
- CCTextureCache::sharedTextureCache()->addImageAsync("HelloWorld.png", this, callfuncO_selector(Assets::loadingCallBack));
- }
- bRet = true;
CC_BREAK_IF(! CCLayer::init());
//取得屏幕大小
CCSize size = CCDirector::sharedDirector()->getWinSize();
//文字标签,用于显示Loding字样和加载的进度
m_pLabelLoading = CCLabelTTF::create("Loading...", "Arial", 25);
m_pLabelPercent = CCLabelTTF::create("0%", "Arial", 25) ;
m_pLabelLoading->setPosition(ccp(size.width / 2, size.height / 2 + 50));
m_pLabelPercent->setPosition(ccp(size.width / 2, size.height / 2));
this->addChild(m_pLabelLoading);
this->addChild(m_pLabelPercent);
//加载HelloWorld.png纹理
for (int i = 0; i < 500; i++)
{
//加载纹理,回调Assets::loadingCallBack
CCTextureCache::sharedTextureCache()->addImageAsync("HelloWorld.png", this, callfuncO_selector(Assets::loadingCallBack));
}
bRet = true;
回调函数loadingCallBack的实现:
- void Assets::loadingCallBack(cocos2d::CCObject *obj)
- {
- //currentNum初始值为0,每加载一张纹理值自加1
- ++currentNum;
- char tmp[10];
- //求得百分比
- sprintf(tmp, "%d%%", (int)(((float)currentNum / totalNum) * 100));
- //设置标签显示的内容
- m_pLabelPercent->setString(tmp);
- //如果当前加载的张数跟总数相等则进行场景跳转
- if (currentNum == totalNum)
- {
- this->removeChild(m_pLabelPercent, true);
- this->removeChild(m_pLabelLoading, true);
- //场景跳转
- CCScene *m_SecondScene = SecondScene::scene();
- CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(1.0f,m_SecondScene));
- }
- }
void Assets::loadingCallBack(cocos2d::CCObject *obj)
{
//currentNum初始值为0,每加载一张纹理值自加1
++currentNum;
char tmp[10];
//求得百分比
sprintf(tmp, "%d%%", (int)(((float)currentNum / totalNum) * 100));
//设置标签显示的内容
m_pLabelPercent->setString(tmp);
//如果当前加载的张数跟总数相等则进行场景跳转
if (currentNum == totalNum)
{
this->removeChild(m_pLabelPercent, true);
this->removeChild(m_pLabelLoading, true);
//场景跳转
CCScene *m_SecondScene = SecondScene::scene();
CCDirector::sharedDirector()->replaceScene(CCTransitionJumpZoom::create(1.0f,m_SecondScene));
}
}
4.新建SecondScene.h和SecondScene.cpp两个文件,用于加载完成后跳转
新建场景的步骤详见http://blog.csdn.net/zlqqhs/article/details/9245125
将SecondScene.cpp文件下的init函数下的代码改为:
- //通过名字从CCTextureCache中取得纹理
- CCTexture2D *texture = CCTextureCache::sharedTextureCache()->textureForKey("HelloWorld.png");
- //实例化精灵
- CCSprite *m_sprite = CCSprite::createWithTexture(texture);
- CC_BREAK_IF(!m_sprite);
- //设置精灵位置
- m_sprite->setPosition(ccp(100, 100));
- //添加精灵到场景
- this->addChild(m_sprite);
- bRet = true;