阅读75 返回首页    go 阿里云 go 技术社区[云栖]


cocos2d-x游戏循环与调度

每一个游戏程序都有一个循环在不断运行它是有导演对象来管理很维护。如果需要场景中的精灵运动起来我们可以在游戏循环中使用定时器Scheduler对精灵等对象的运行进行调度。因为Node类封装了Scheduler类所以我们也可以直接使用Node中调用函数。

Node中调用函数主要有

void scheduleUpdate ( void )。每个Node对象只要调用该函数那么这个Node对象就会定时地每帧回调用一次自己的update(float dt)函数。

void schedule ( SEL_SCHEDULE selector,  float  interval )。与scheduleUpdate函数功能一样不同的是我们可以指定回调函数通过selector指定也可以更加需要指定回调时间间隔。

void unscheduleUpdate ( void )。停止update(float dt)函数调度。

void unschedule ( SEL_SCHEDULE selector )。可以指定具体函数停止调度。

void unscheduleAllSelectors ( void )。可以停止调度。

 

为了进一步了解游戏循环与调度的使用我们修改HelloWorld实例。

修改HelloWorldScene.h代码添加update(float dt)声明代码如下

class HelloWorld : public cocos2d::Layer
{
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停止。

 

更多内容请关注最新Cocos图书《Cocos2d-x实战 C++卷》
本书交流讨论网站https://www.cocoagame.net
更多精彩视频课程请关注智捷课堂Cocos课程https://v.51work6.com
欢迎加入Cocos2d-x技术讨论群257760386


《Cocos2d-x实战 C++卷》现已上线各大商店均已开售

京东https://item.jd.com/11584534.html

亚马逊https://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

当当https://product.dangdang.com/23606265.html

互动出版网https://product.china-pub.com/3770734

《Cocos2d-x实战 C++卷》源码及样章下载地址

源码下载地址https://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1 

样章下载地址https://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

欢迎关注智捷iOS课堂微信公共平台

最后更新:2017-04-03 12:56:32

  上一篇:go cocos2d-x中Node与Node层级架构
  下一篇:go cocos2d-x一些核心概念介绍