如果是用于cocos2dx 本身的动画需要,还是用
void schedule(const std::function<void(float)>& callback, float interval, unsigned int repeat, float delay, const std::string &key);
(这里需要着重理解下两个参数:interval 和 delay,delay:秒,首次调用callback时的延时,也就是延时多少秒来开始这整个的延时调用。interval:秒,后面的每次循环时间间隔。)
来实现,如:
int count = 0;
schedule([&](float dt)
{
if (count > 3) //设置个计数,如果达到后把计时器取消掉
{
unschedule("MySchedule");
}
else
{
count ++;
//your codes below
//....
}
}, 1.f, CC_REPEAT_FOREVER, 0.0f, "MySchedule"); //同时也可以注意下这里的参数 CC_REPEAT_FOREVER
或者这样:
int count = 0;
schedule(schedule_selector(HelloWorld::delayCall),1.f,CC_REPEAT_FOREVER,0.0f);
void HelloWorld::delayCall(float dt)
{
if(count > 3){
unschedule(schedule_selector(HelloWorld::delayCall));
}else{
//do something here
}
}