您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx三种定时器的使用

2015-10-28 19:40 495 查看
cocos2dx三种定时器的使用以及停止schedule,scheduleUpdate,scheduleOnce

今天白白跟大家分享一下cocos2dx中定时器的使用方法。

首先,什么是定时器呢?或许你有时候会想让某个函数不断的去执行,或许只是执行一次,获取你想让他每隔几秒执行一次,ok,这些都可以统统交给定时器来解决。

cocos2dx中有三种定时器:schedule,scheduleUpdate,scheduleOnce。了解其功能便会发现定时器真是太方便了,废话不多说,我们逐一学习一下。

1、scheduleUpdate

加入当前节点后,程序会每帧都会自动执行一次默认的Update函数。(注:一定是Update函数哦,若想调用其他自己命名的函数则使用schedule)

看例子,走起。

首先在HelloWord类的头文件中声明Update函数:

[cpp] view
plaincopyprint?





void Update(float dt); //注意参数类型

[cpp] view
plaincopyprint?





void Update(float dt); //注意参数类型

然后在HelloWorld类源文件中实现函数Update:

[cpp] view
plaincopyprint?





void HelloWorld::Update(float dt)

{

CCLOG("baibai");

}

[cpp] view
plaincopyprint?





void HelloWorld::Update(float dt)

{

CCLOG("baibai");

}

现在我们可以调用了,在需要他不断执行的地方加入调用的代码就ok:

[cpp] view
plaincopyprint?





this->scheduleUpdate(); //this是当前节点,如layer,所以可以省略啦。

[cpp] view
plaincopyprint?





this->scheduleUpdate(); //this是当前节点,如layer,所以可以省略啦。

运行之后你将会看到不断有baibai被打印出来
2、scheduleUpdate

可以没隔几秒执行某个自定义的函数,来看代码:

首先还是在HelloWorld中声明所要执行的函数:

[cpp] view
plaincopyprint?





void Move(float dt);

[cpp] view
plaincopyprint?





void Move(float dt);

然后在源文件实现:

[cpp] view
plaincopyprint?





void HelloWorld::Move(float dt)

{

CCLOG("baibai");

}

[cpp] view
plaincopyprint?





void HelloWorld::Move(float dt)

{

CCLOG("baibai");

}

现在去执行他,注意参数哦

[cpp] view
plaincopyprint?





scheduleOnce(schedule_selector(HelloWorld::Move), 1.0f);//每隔1.0f执行一次,省略参数则表示每帧都要执行

[cpp] view
plaincopyprint?





scheduleOnce(schedule_selector(HelloWorld::Move), 1.0f);//每隔1.0f执行一次,省略参数则表示每帧都要执行



运行之后,baibai每隔1.0f才会被打印一次。
3、scheduleOnce

功能:在几秒之后执行,并且只执行一次。

我们就执行上面所写过的Move函数吧:

[cpp] view
plaincopyprint?





scheduleOnce(schedule_selector(HelloWorld::Move), 1.0f); //在1.0f之后执行,并且只执行一次。

[cpp] view
plaincopyprint?





scheduleOnce(schedule_selector(HelloWorld::Move), 1.0f); //在1.0f之后执行,并且只执行一次。

运行一下,baibai只是被打印了一次就完了。。。
ok,定时器的调用已经讲完,大家不妨自己写一些函数体验一下。

但是怎么让定时器停止呢?

1、停止执行自己定义函数的定时器:

[cpp] view
plaincopyprint?





this->unschedule(schedule_selector(HelloWorld::Move));

[cpp] view
plaincopyprint?





this->unschedule(schedule_selector(HelloWorld::Move));

2、停止默认定时器:

[cpp] view
plaincopyprint?





this->unscheduleUpdate();

[cpp] view
plaincopyprint?





this->unscheduleUpdate();

3、停止所有定时器:

[cpp] view
plaincopyprint?





this->unscheduleAllSelectors();

[cpp] view
plaincopyprint?





this->unscheduleAllSelectors();


1.概况

CCNode内部封装了一个

[cpp] view plaincopy

CCScheduler *m_pScheduler;

正是通过它我们可以很轻松地完成一些定时功能,所以定时器是节点所具备的功能。

定时器分为2种,一种是更新定时器,执行的频率是每帧执行一次,另一种则是自定义回调函数的定时器(最小值是一帧),关于回调函数和函数指针的相关基础可参见/article/1340663.html


2.API

[cpp] view plaincopy

//更新定时器,每帧调用1次。每个节点只能有1个被调度的update函数

void scheduleUpdate(void);

//卸载更新定时器

void unscheduleUpdate(void);

//自定义定时器,如果重复调用,那调用间隔会更新,而不会再次调用

//interval,调用时间间隔,如果为0,建议使用scheduleUpdate

//repeat,回调函数会被执行repeat+1次,kCCRepeatForever是无限次调用

//delay,第一次执行前的延时

void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);

void schedule(SEL_SCHEDULE selector, float interval);

void scheduleOnce(SEL_SCHEDULE selector, float delay);

void schedule(SEL_SCHEDULE selector);

//卸载自定义定时器

void unschedule(SEL_SCHEDULE selector);

void unscheduleAllSelectors(void);

//恢复所有定时器和动作,OnEnter调用

void resumeSchedulerAndActions(void);

//暂停所有定时器和动作,OnExit调用

void pauseSchedulerAndActions(void);

//scheduleUpdate每帧调用

virtual void update(float delta);


3.示例


3.1.更新定时器

[cpp] view plaincopy

//开启定时器

this->scheduleUpdate();

//虚函数update

void HelloWorld::update(float delta)

{

CCLog("%f",delta);

}

//输出,这里设置了60fps,调用间隔1/60s

0.016667

0.016676

0.016657

0.016669


3.2.自定义定时器

[cpp] view plaincopy

//开启定时器,延时2s执行,执行3+1次,执行间隔1s

this->schedule(schedule_selector(HelloWorld::log),1,3,2);

//回调函数

void HelloWorld::log(float dt)

{

CCLog("schedule");

}

//输出

2.004532

1.005827

1.000238

1.001019


4.schedule_selector和SEL_SCHEDULE

看到上面的schedule_selector了吧,这又是个什么玩意?看看它的宏定义。

[cpp] view plaincopy

#define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR)

原来是把函数指针转化为SEL_SCHEDULE型指针,那SEL_SCHEDULE又是什么?

[cpp] view plaincopy

typedef void (CCObject::*SEL_SCHEDULE)(float);

也没啥,就是定义了一个带有float参数函数指针。所以我们在使用自定义Schedule的时候,回调函数一定要记得带上一个float参数,它记录了两次执行的间隔。如果忘了,可是会出现类型转换错误的异常。这种方式在callfunc_selector,menu_selector等也以同样的方式出现。


5.谁来调用回调函数

但是有没有发现,如果这个回调函数是个全局函数或者static函数也就算了,偏偏它是个成员函数,成员函数需要实例来调用,可是从调用方法来看,好像没传入调用对象?

[cpp] view plaincopy

this->schedule(schedule_selector(HelloWorld::log),1,3,2);

是的,还记得一开头说的CCNode内部封装的m_pScheduler吗?

[cpp] view plaincopy

CCScheduler *m_pScheduler;

原来CCNode帮我们实现了:

[cpp] view plaincopy

void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)

{

CCAssert( selector, "Argument must be non-nil");

CCAssert( interval >=0, "Argument must be positive");

m_pScheduler->scheduleSelector(selector, this, interval , repeat, delay, !m_bRunning);

}

原来this这个时候被传入了,同时传入的参数还有m_bRunning,m_bRunning表示节点是否在运行中(是否在舞台上),OnEnter的时候赋值true,OnExit的时候赋值false,所以在执行定时器的时候还必须确保节点有在运行。

这样确实用起来怪怪的,所以在cocos2d-x v3.0版本中,参数和函数指针用一个宏打包起来了~
至于CCSchedule内部是怎么实现的,以及CCTimer的触发回调,有兴趣的就自己看看源码吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: