关于加载游戏页面的方法
一般游戏的时候,或者是一些比较大型的游戏的时候,游戏的资源会比较多,有时候不同的场景所用的图片、音乐等等都是不一样的。为了更加方便的加载资源和减轻系统的负担。因此我们一般会有一个加载游戏资源的页面。也就是loading页面。
接下来我就分享一下一个简单加载页面的制作。
首先我们在.h文件里面声明,然后写一个load的加载函数:
ProgressTimer*_progress; //进度条
float_progressInterval; //进度条的更新次数
float_sourceCount;//用来保存游戏加载的总资源数
voidload(float dt); //刷新资源
在函数中:
voidHelloWorld::load(floatdt)
{
autoac=ProgressTo::create(1,100-_progressInterval-_sourceCount);
_progress->runAction(ac);
if(_sourceCount<0)
{
autoscene1=Start::createScene();
Director::getInstance()->replaceScene(scene1);
CCLOG("资源加载完毕");
_progress->stopAllActions();
}
_sourceCount--;
}
每一次进行刷新之后资源数量减一,到零才结束。
然后在页面初始化函数中新建进度条和调用这个时间刷新资源函数。
代码如下:
autobar=Sprite::create("bloodfull.png");
_progress=ProgressTimer::create(bar);
_progress->setType(ProgressTimer::Type::BAR);
_progress->setMidpoint(Point(0,0.5));
_progress->setBarChangeRate(Point(1,0));
_progress->setAnchorPoint(Point(0,1));
_progress->setPercentage(0.0f);
_progress->setPosition(visibleSize.width/2,200);
this->addChild(_progress);
_sourceCount=10;
_progressInterval=100.0/(float)_sourceCount;
this->schedule(schedule_selector(HelloWorld::load),1.0f);
我假定资源有十个,这样运行之后你就可以看到场景的资源加载啦!同时你要新建一个场景,这样才能再加载完成之后进入到下一个游戏页面。
这个比较实用哦!
谢谢大家。