1、首先在GameScene.h头文件声明一个int类型的时间变量int nTime,同时声明一个倒计时功能的函数void timeReduce(float time);
2、在GameScene.cpp函数前声明Label * pAlarmClockLB;
3、Init方法中创建时间标签和时间的图标,并且打开时间调度器
//创建时间标签
pAlarmClockLB=Label::createWithSystemFont("","Marker Felt",32);
pAlarmClockLB->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
pAlarmClockLB->setTextColor(ccColor4B(255,0,0,0));
nTime = 10;
this->addChild(pAlarmClockLB,2);
//时间的图标
auto pAlarmClock = Sprite::create("timing.png");
pAlarmClock->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
this->addChild(pAlarmClock);
this->schedule(schedule_selector(GameScene::timeReduce),1.0f);//时间调度器
4、实现倒计时功能函数
void GameScene::timeReduce(float time)
{
nTime--;
char t1[10];
sprintf(t1,"%d",nTime);
pAlarmClockLB->setString(t1);
CCLOG("%d\n",nTime);
if (nTime == 0)
{
this->unschedule(schedule_selector(GameScene::timeReduce));//关闭时间调度器
MessageBox("NO TIME!!!", "Tips");
}
}
附加:倒计时时间表摇摆动作
auto rotateToRight = RotateTo::create(0.1,10);
auto rotateToLeft = RotateTo::create(0.1,-10);
auto seq=Sequence::create(rotateToRight,rotateToLeft,NULL);
pAlarmClockSP->runAction(RepeatForever::create(seq));