http://blog.csdn.net/start530/article/details/19420317 本来这篇博文是昨晚就要写的,可是因为今早要去参加考驾照相关的体检,而我最害怕的就是视力没能达到5.0,毕竟这阶段对着屏幕的时间过久。 所以呢,昨晚我几乎没碰电脑,没玩手机,早睡早起。体检顺
http://blog.csdn.net/start530/article/details/19420317
本来这篇博文是昨晚就要写的,可是因为今早要去参加考驾照相关的体检,而我最害怕的就是视力没能达到5.0,毕竟这阶段对着屏幕的时间过久。
所以呢,昨晚我几乎没碰电脑,没玩手机,早睡早起。体检顺利通过!首先看下头文件:HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { public: HelloWorld():m_numSp(20),m_loadedSp(0),loadProgress(NULL){}; static cocos2d::Scene* createScene(); virtual bool init(); void loadingCallback(Object* pSender);//加载一张图片完成后跳转的毁掉函数 void gotoNewLayer();//加载完后的跳转函数 CREATE_FUNC(HelloWorld); private: cocos2d::ProgressTimer* loadProgress;//进度条 cocos2d::LabelTTF* percentLabel;//加载进度label cocos2d::LabelTTF* loadLabel;//显示 loading: 的label int m_numSp;//要加载的精灵数目,初始化为 20 张 int m_loadedSp;//已加载的精灵数目 }; #endif // __HELLOWORLD_SCENE_H__
Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); loadLabel = LabelTTF::create("Loading:","Arial",20);//创建显示Loading: 的label loadLabel->setPosition(Point(visibleSize.width/2-30,visibleSize.height/2+30)); this->addChild(loadLabel,1); percentLabel = LabelTTF::create("0%","Arial",20);//创建显示百分比的label percentLabel->setPosition(Point(visibleSize.width/2+35,visibleSize.height/2+30)); this->addChild(percentLabel,2); auto loadBg = Sprite::create("sliderTrack.png");//进程条的底图 loadBg->setPosition(Point(visibleSize.width/2,visibleSize.height/2)); this->addChild(loadBg,1); loadProgress = ProgressTimer::create(Sprite::create("sliderProgress.png"));//创建一个进程条 loadProgress->setBarChangeRate(Point(1,0));//设置进程条的变化速率 loadProgress->setType(ProgressTimer::Type::BAR);//设置进程条的类型 loadProgress->setMidpoint(Point(0,1));//设置进度的运动方向 loadProgress->setPosition(Point(visibleSize.width/2,visibleSize.height/2)); loadProgress->setPercentage(0.0f);//设置初始值为0 this->addChild(loadProgress,2);
//加载20张图片,每加载完一张就调用回调函数:loadingCallback Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld1.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld2.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld3.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld4.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld5.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld6.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld7.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback)); Director::getInstance()->getTextureCache()->addImageAsync("HelloWorld.png",this,callfuncO_selector(HelloWorld::loadingCallback));
void HelloWorld::loadingCallback(Object* pSender) { ++m_loadedSp;//每进到这个函数一次,让m_loadedSp + 1 char buf_str[16]; sprintf(buf_str,"%d%%",(int)(((float)m_loadedSp / m_numSp) * 100),m_numSp); percentLabel->setString(buf_str);//更新percentLabel的值 float newPercent = 100 - ((float)m_numSp - (float)m_loadedSp)/((float)m_numSp/100);//计算进度条当前的百分比 //因为加载图片速度很快,所以就没有使用ProgressTo, //或者ProgressFromTo这种动作来更新进度条 loadProgress->setPercentage(newPercent);//更新进度条 //图片加载完成后 if(m_loadedSp == m_numSp) { this->removeChild(loadProgress);//将添加的几个对象删除掉 this->removeChild(percentLabel); this->removeChild(loadLabel); //加载完既要跳转到gotoNewLayer,在这里可以 //创建新的Scene,新的Layer,或者其他什么乱七八糟的 this->gotoNewLayer(); } }
void HelloWorld::gotoNewLayer() { auto size = Director::getInstance()->getWinSize(); auto sp = Sprite::create("HelloWorld.png");//用之前加载到缓存中的图片,创建一个精灵。 sp->setPosition(Point(size.width/2,size.height/2)); this->addChild(sp,1); }