cocos2d-x提供了CCProgressTimer类可以方便地实现进度条的功能,cocos2d-x 1.x版本与cocos2d-x 2.x版本对ProgressTimer的类型提供的不同,2.x版本只提供了以下2种:
typedef enum {
/// Radial Counter-Clockwise
kCCProgressTimerTypeRadial,
/// Bar
kCCProgressTimerTypeBar,
} CCProgressTimerType;
另外,cocos2d-iphone与cocos2d-x除了实现语言不同外,还有很大的差异,比如cocos2d-x初始化一个实例时用的是create()函数,cocos2d用的是各种initwithxxx,cocos2d中的OC语法中的存取器容易误导C++出身的程序员,例如点操作符表示调用getter或setter方法,而不是C++中的访问public数据成员。
例子中代码功能为:
虚拟一个资源加载的进度条,一红一绿,分别增加和减少,加载完毕后,会运行一个颜色渐变的切换场景,然后进入主场景,
以下为具体代码,老习惯,不码字,看注释:
1. 头文件,其中有2个私有数据成员,分别为进度条1和进度条2:
#include "cocos2d.h"
class LoadGame : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
void LoadGame::update(float dt);
// implement the "static node()" method manually
CREATE_FUNC(LoadGame);
private:
cocos2d::CCProgressTimer *progresstime1;
cocos2d::CCProgressTimer *progresstime2;
};
#endif // __HELLOWORLD_SCENE_H__
2. LoadGame类具体实现
//创建二个精灵,一绿一红
CCSprite *psSprite1 = CCSprite::create("green.png");
CCSprite *psSprite2 = CCSprite::create("red.png");
//利用精灵创建进度条,并设置一些属性
progresstime1 = CCProgressTimer::create(psSprite1); //初始化CCProgressTimer
progresstime1->setPercentage(0.0f); //设置初始百分比的值
progresstime1->setScale(3); //设置进度条大小为原始的3倍
progresstime1->setBarChangeRate(ccp(1, 0)); //设置进度条的长度和高度开始变化的大小
progresstime1->setType(kCCProgressTimerTypeBar); //设置进度条为水平
progresstime1->setPosition(ccp(size.width/2, size.height/2)); //放置进度条位置
this->addChild(progresstime1, 100); //加入Layer中
//利用精灵创建进度条,并设置一些属性
progresstime2 = CCProgressTimer::create(psSprite2); //初始化CCProgressTimer
progresstime2->setPercentage(100.0f); //设置初始百分比的值
progresstime2->setScale(3); //设置进度条大小为原始的3倍
progresstime2->setBarChangeRate(ccp(1, 0)); //设置进度条的长度和高度开始变化的大小
progresstime2->setType(kCCProgressTimerTypeBar); //设置进度条为水平
progresstime2->setPosition(ccp(size.width/2, size.height/2 - 30)); //放置进度条位置
this->addChild(progresstime2, 101); //加入Layer中
this->scheduleUpdate(); //调用定时器更新进度条
3. 定时器的具体实现:
void LoadGame::update(float dt)
{
//CCProgressTimer *progresstime = static_cast(this->getChildByTag(100));
float ct1 = progresstime1->getPercentage(); //取得当前进度的百分比
float ct2 = progresstime2->getPercentage(); //取得当前进度的百分比
ct1 = ct1 + 0.5f; //每帧+0.5%
ct2 = ct2 - 0.5f;
//如果进度条小于100%,设置进度条的百分比
if (ct1 <= 100)
{
CCLOG("progresstime1:%f, progresstime2:%f", ct1, ct2);
progresstime1->setPercentage(ct1);
progresstime2->setPercentage(ct2);
}
//如果进度条达到100%,则进入过渡场景,过渡场景会在2秒后进入主场景
else
{
CCTransitionFade *tScene = CCTransitionFade::create(2, HelloWorld::scene(), ccWHITE);
CCDirector::sharedDirector()->replaceScene(tScene);
}
}
opengl版本是否支持:
<= GLES_VER_1_0如果为真则为不支持
- CCTransitionCrossFade::transitionWithDuration(t,s);//淡出淡入交叉,同时进行
- CCTransitionRadialCCW::transitionWithDuration(t,s);//顺时针切入
- CCTransitionRadialCW::transitionWithDuration(t,s);//逆时针切入