倒计时功能

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值