CCTextureCache::sharedTextureCache()->addImageAsync(char*
fileName,CCObject*
target,SEL_CallfuncO
selector);
此方法将图片文件加载进缓存,并且在加载完成后调用一个回调函数。
首先先创建一个进度条loadingImage:
CCSize size =
CCDirector::sharedDirector()->getWinSize();
CCSprite* temp =
CCSprite::create("xxx.png");
temp->setAnchorPoint(ccp(0,0));
CCProgressTimer*loadingImage
= CCProgressTimer::create(temp);
this->addChild(loadingImage);
loadingImage->setPosition(ccp(size.width/2,
size.height/2));
loadingImage->setType(kCCProgressTimerTypeBar);//设置进度条类型,选用bar类型
loadingImage->setMidpoint(ccp(0,0.5));//设置进度条起始点,范围是(0,0)~(1,1)
loadingImage->setBarChangeRate(ccp(1,0))//设置进度条的变化速率
loadingImage->setTag(100);
loadingImage->retain();
第二步需要创建我们控制loading进度条的回调方法
void Loading::callBackEvent(CCObject*
object)
{
static int progressCount =
0;//设置一个静态计数器,记录使用回调方法的次数
CCProgressTimer* loadingImage
= (CCProgressTimer
*)this->getChildByTag(100);
loadingImage->stopAllActions();
progressCount++;//计数加1
CCProgressTo* to =
CCProgressTo::create(0.2f,100 / i * progressCount);//参数i为一个int类型的变量,他表示需要加载的图片数。
loadingImage->runAction(to);//运行进度条动画
if (i == progressCount)//当回调方法的调用次数达到最后一次时,将场景加载进来
{
CCDirector::shareDirector()->replaceScene(nextScene);
}
}
最后我们只需要依次调用addImageAsync这个方法就行了
CCTextureCache::sharedTextureCache()->addImageAsync("xxx_1.png",this,callfuncO_selector(Loading::callBackEvent));
CCTextureCache::sharedTextureCache()->addImageAsync("xxx_1.png",this,callfuncO_selector(Loading::callBackEvent));
CCTextureCache::sharedTextureCache()->addImageAsync("xxx_1.png",this,callfuncO_selector(Loading::callBackEvent));CCTextureCache::sharedTextureCache()->addImageAsync("xxx_1.png",this,callfuncO_selector(Loading::callBackEvent));
CCTextureCache::sharedTextureCache()->addImageAsync("xxx_1.png",this,callfuncO_selector(Loading::callBackEvent));
CCTextureCache::sharedTextureCache()->addImageAsync("xxx_1.png",this,callfuncO_selector(Loading::callBackEvent));