转自:http://blog.csdn.net/xiaoxiangp/article/details/7706764
昨天下载了最新的cocos2d-x 是2.0.1版本的...所以本文章是参考2.0版本的例子写的...
可以发现,2.0版本后不单单只有一个帧率了..还多了两个参数.第一个参数可以理解为场景中的对象个数吧.第二个还不清楚...
1. 创建好一个工程后,先修改对于的.h文件
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene();
- HelloWorld();
- void addSprite();//用于加载完成后添加精灵
- void loadingCallBack(cocos2d::CCObject *obj);//读进度回调函数
- LAYER_CREATE_FUNC(HelloWorld);
- private:
- cocos2d::CCLabelTTF *m_pLableLoading;
- cocos2d::CCLabelTTF *m_pLabelPercent;
- int m_nNumberOfSprites;
- int m_nNumberOfLoadedSprites;
- };
2. 然后在对应的cpp文件中完成各种函数
- //原例子中的代码都是在构造函数中写的.在这里构造函数只用于初始化了两个成员变量
- HelloWorld::HelloWorld():m_nNumberOfSprites(20),m_nNumberOfLoadedSprites(0)
- {
- }
- bool HelloWorld::init()
- {
- bool bRet = false;
- do
- {
- CC_BREAK_IF(! CCLayer::init());
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- //创建读条时的标签
- m_pLableLoading = CCLabelTTF::create("Loading...","Arial",20);
- m_pLabelPercent = CCLabelTTF::create("%0","Arial",30);
- m_pLabelPercent->setPosition(CCPointMake(size.width/2, size.height/2 +50));
- m_pLableLoading->setPosition(CCPointMake(size.width/2, size.height/2 -10));
- this->addChild(m_pLableLoading);
- this->addChild(m_pLabelPercent);
- //加载图片到Cache...每加载一次就调用我们自己写的回调函数
- CCTextureCache::sharedTextureCache()->addImageAsync("img/bg.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/01.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/02.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/03.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/04.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/05.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/06.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/07.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/08.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/09.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/10.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/11.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/12.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/13.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/14.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/15.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/16.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/17.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/18.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- CCTextureCache::sharedTextureCache()->addImageAsync("img/19.png", this, callfuncO_selector(HelloWorld::loadingCallBack));
- bRet = true;
- } while (0);
- return bRet;
- }
3. 代码很多都是重复的..只是加载的图片不一样,大家注意加载的图片路径就可以了..接下来实现回调函数
- //回调函数
- void HelloWorld::loadingCallBack(CCObject *obj)
- {
- ++m_nNumberOfLoadedSprites;//每调用一次就+1
- char tmp[10];
- //这里格式化,因为我们只加载了20张图片,所以除以总数后就是0.05,所以每加载一次就涨5%
- sprintf(tmp,"%%%d",(int)(((float)m_nNumberOfLoadedSprites / m_nNumberOfSprites) * 100));
- m_pLabelPercent->setString(tmp);//重设标签的内容
- //判断,如果全部加载完成后,就执行
- if (m_nNumberOfLoadedSprites == m_nNumberOfSprites)
- {
- //移除那两个标签文字..
- this->removeChild(m_pLableLoading, true);
- this->removeChild(m_pLabelPercent, true);
- //调用自己写的函数..从新绘制场景内容
- addSprite();
- }
- }
4. 实现addSprite()函数,这个函数从新初始化了场景..然后显示
- void HelloWorld::addSprite()
- {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- //创建背景图片
- CCSprite *bg = CCSprite::create("img/bg.png");
- bg->setPosition(CCPointMake(size.width/2, size.height/2));
- //创建其他小图片
- CCSprite *s1 = CCSprite::create("img/01.png");
- CCSprite *s2 = CCSprite::create("img/02.png");
- CCSprite *s3 = CCSprite::create("img/03.png");
- CCSprite *s4 = CCSprite::create("img/04.png");
- CCSprite *s5 = CCSprite::create("img/05.png");
- CCSprite *s6 = CCSprite::create("img/06.png");
- CCSprite *s7 = CCSprite::create("img/07.png");
- CCSprite *s8 = CCSprite::create("img/08.png");
- CCSprite *s9 = CCSprite::create("img/09.png");
- CCSprite *s10 = CCSprite::create("img/10.png");
- CCSprite *s11 = CCSprite::create("img/11.png");
- CCSprite *s12 = CCSprite::create("img/12.png");
- CCSprite *s13 = CCSprite::create("img/13.png");
- CCSprite *s14 = CCSprite::create("img/14.png");
- CCSprite *s15 = CCSprite::create("img/15.png");
- CCSprite *s16 = CCSprite::create("img/16.png");
- CCSprite *s17 = CCSprite::create("img/17.png");
- CCSprite *s18 = CCSprite::create("img/18.png");
- CCSprite *s19 = CCSprite::create("img/19.png");
- //设置位置
- s1->setPosition(CCPointMake(70, size.height/2));
- s2->setPosition(CCPointMake(90, size.height/2));
- s3->setPosition(CCPointMake(110, size.height/2));
- s4->setPosition(CCPointMake(130, size.height/2));
- s5->setPosition(CCPointMake(150, size.height/2));
- s6->setPosition(CCPointMake(170, size.height/2));
- s7->setPosition(CCPointMake(190, size.height/2));
- s8->setPosition(CCPointMake(210, size.height/2));
- s9->setPosition(CCPointMake(230, size.height/2));
- s10->setPosition(CCPointMake(250, size.height/2));
- s11->setPosition(CCPointMake(270, size.height/2));
- s12->setPosition(CCPointMake(290, size.height/2));
- s13->setPosition(CCPointMake(310, size.height/2));
- s14->setPosition(CCPointMake(330, size.height/2));
- s15->setPosition(CCPointMake(350, size.height/2));
- s16->setPosition(CCPointMake(370, size.height/2));
- s17->setPosition(CCPointMake(390, size.height/2));
- s18->setPosition(CCPointMake(410, size.height/2));
- s19->setPosition(CCPointMake(430, size.height/2));
- //添加进入场景
- this->addChild(bg);
- this->addChild(s1);
- this->addChild(s2);
- this->addChild(s3);
- this->addChild(s4);
- this->addChild(s5);
- this->addChild(s6);
- this->addChild(s7);
- this->addChild(s8);
- this->addChild(s9);
- this->addChild(s10);
- this->addChild(s11);
- this->addChild(s12);
- this->addChild(s13);
- this->addChild(s14);
- this->addChild(s15);
- this->addChild(s16);
- this->addChild(s17);
- this->addChild(s18);
- this->addChild(s19);
- }
这样,运行的时候就会出现读条界面了,但是闪的很快,比较电脑的处理能力还是很快的嘛..可以在回调函数里面设置一个断点,就会发现是5%的增长..
就我的理解来说,这一个例子的思路,首先是把图片加载进入内存.然后出发一次回调函数,更新读条进度..直到100%就调用另外函数...
图片的话,随便弄个几张就好了.显示效果的话,注意设置位置就行,也可以用我的图片啦:
http://115.com/file/anpzdgqa#img.rar
by...XX