转载请标明:转载自【小枫栏目】,博文链接:http://blog.csdn.net/my183100521/article/details/10236165
一、cocos2d-x 常用定时函数:scheduleUpdate,schedule,scheduleOnce;
二、使用方法
1. scheduleUpdate
1.1 使用方法
this->scheduleUpdate()
1.2 重载update(float fDelta),因为游戏节点在游戏运行的每一帧被调用一次update函数。
HelloWorldScene.h
virtual void update(float fDelta);
HelloWorldScene.cpp
void HelloWorld::update(float fDelta)
{
CCLog("HelloWorld");
}
1.3 取消定时器
this->unscheduleUpdate();
2. schedule
2.1 使用方法
this->schedule(schedule_selector(HelloWorld::function),agr0) ;//function是定时调用函数,agr0是每隔几秒调用一次function函数
2.2 取消定时器
this->unschedule(schedule_selector(function));//function是被定时器调用的函数
3. scheduleOnce,即是解发器
3.1使用方法
this->scheduleOnce(schedule_selector(HelloWorld:: function), agr0);//function是被触发函数,agr0是延时几秒后调用function函数
4.取消所有定时器
this->unscheduleAllSelectors();
三、测试代码
HelloWorldScene.cpp
/* 常用定时器 */
this->scheduleUpdate();
/* 指定每帧执行自定义的函数 */
this->schedule(schedule_selector(HelloWorld::MutUpdate),0.1f);
/* 延时几秒触发函数 */
//this->scheduleOnce(schedule_selector(HelloWorld::MutUpdate), 2.0f);
被定时函数
void HelloWorld::MutUpdate(float fDelta)
{
CCLog("Hello Kity");
this->unscheduleAllSelectors();
}
void HelloWorld::MutOnceUpdate(float fDelta)
{
CCLog("Hello Boy");
}
四、测试结果
Cocos2d: HelloWorld
Cocos2d: HelloWorld
Cocos2d: Hello Kity
引用博文: http://www.benmutou.com/blog/archives/58