Node中调用函数主要有:html
void scheduleUpdate ( void )。每一个Node对象只要调用该函数,那么这个Node对象就会定时地每帧回调用一次本身的update(float dt)函数。函数
void schedule ( SEL_SCHEDULE selector, float interval )。与scheduleUpdate函数功能同样,不一样的是咱们能够指定回调函数(经过selector指定),也能够更加须要指定回调时间间隔。this
void unscheduleUpdate ( void )。中止update(float dt)函数调度。spa
void unschedule ( SEL_SCHEDULE selector )。能够指定具体函数中止调度。.net
void unscheduleAllSelectors ( void )。能够中止调度。code
为了进一步了解游戏循环与调度的使用,咱们修改HelloWorld实例。orm
修改HelloWorldScene.h代码,添加update(float dt)声明,代码以下:htm
[html] view plaincopy对象
class HelloWorld : public cocos2d::Layer blog
{
public:
... ...
virtual void update(float dt);
CREATE_FUNC(HelloWorld);
};
修改HelloWorldScene.cpp代码以下:
bool HelloWorld::init()
{
... ...
auto label = LabelTTF::create("Hello World","Arial", 24);
label->setTag(123); ①
... ...
//更新函数
this->scheduleUpdate(); ②
//this->schedule(schedule_selector(HelloWorld::update),1.0f/60); ③
return true;
}
voidHelloWorld::update(float dt) ④
{
auto label =this->getChildByTag(123); ⑤
label->setPosition(label->getPosition()+ Point(2,-2)); ⑥
}
void HelloWorld::menuCloseCallback(Ref*pSender)
{
//中止更新
unscheduleUpdate(); ⑦
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM ==CC_PLATFORM_IOS)
exit(0);
#endif
}
为了可以在init函数以外访问标签对象label,咱们须要为标签对象设置Tag属性,其中的第①行代码就是设置Tag属性为123。第⑤行代码是经过Tag属性得到从新得到这个标签对象。
为了可以开始调度还须要在init函数中调用scheduleUpdate(见第②行代码)或schedule(见第③行代码)。
代码第④行的HelloWorld::update(floatdt)函数是在调度函数,精灵等对象的变化逻辑都是在这个函数中编写的。咱们这个例子很简单只是让标签对象动起来,第⑥行代码就是改变它的位置。
为了省电等目的,若是再也不使用调度,必定不要忘记中止调度。第⑦行代码unscheduleUpdate()就是中止调度update,若是是其余的调度函数能够采用unschedule或unscheduleAllSelectors中止。