Cocos2dx 3.0 过渡篇(十)资源加载进度条Loading...

尊重原创,转载请注明来自:star特530的CSDN博客 http://blog.csdn.net/start530/article/details/19420317


本来这篇博文是昨晚就要写的,可是因为今早要去参加考驾照相关的体检,而我最害怕的就是视力没能达到5.0,毕竟这阶段对着屏幕的时间过久。

所以呢,昨晚我几乎没碰电脑,没玩手机,早睡早起。体检顺利通过!


首先,我要说的是:这次我要写的主题是进度条。 额,等等,先收起你手里愤怒的西瓜刀。我也才知道TestCpp也有这个例子啊。不过TestCpp里的只有label的变化,而我的多加了个进度条。
请容我对我的这种手段取个好听的名称:画龙点睛!


恩,步骤如下:
1、创建label和progressTimer;
2、加载资源,每加载一张都调用回调函数;
3、加载完成,进入新的界面。



首先看下头文件: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__

1、创建

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);

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));

3、图片加载后的回调函数:

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();
	}
}

4、进入新的界面

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);
}


因为代码里注释都写的挺详细的,所以我也就不说太多废话了。
恩,写完了。这篇是下班后加班写的,外面又下了大雨,我要赶紧冲回去吃饭了。 风一般的男纸


尊重原创,转载请注明原址: http://blog.csdn.net/start530?viewmode=contents


你可能感兴趣的:(cocos2d-x学习笔记,Cocos2dx,3.0,过渡篇)