码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-x 3.0final 终结者系列教程09-绘图节点Node中的Schedule

时间:2014-06-05 12:30:30      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x   node 计划任务 schedule   

如何让HelloWorld项目中的HelloWorld文字实现自动运动呢?

有的童鞋会想到使用线程,不断修改Label的Position,

这样不行,因为在Cocos2d-x中只能在主线程中来修改Node中的信息,这是由于所有的node都是非线程安全的,如果我们的场景移除了node

在子线程种可能引用错误,所以,要让Node执行特定的变化,需要在当前的Node中使用Schedule

使用方法很简单

1。在当前的HelloWorldScne.h中添加一个方法在HelloWorldScene

   如:

       void gameLogic(float t);  //注意这里的参数要设置为float

2.在HelloWorldScene::init方法添加以下代码:

    this->schedule(schedule_select(HelloWorldScene::gameLogic),0.5);

  还有把label设置一个tag

  label->setTag(110);


3.在HelloWorldScne.cpp中实现这个方法

     void HelloWorldScene::gameLogic(float t){

       //在这里修改 HelloWorld 对应的Label的坐标

      auto theLabel=this->getChildByTag(110);

      theLabel->runAction(MoveBy::create(5,0.5));

     if(theLable->getPositionX()>480){theLabel->setPositionX(0);}

      }

这样就实现了HelloWorld的自动向右移动,每0.5秒移动5像素。

这里使用了runAction,实际上直接通过theLabel->setPositionX(newX)也可以,我们可以在这里修改Node的各种属性以达到对场景中的Node变换的作用。

Action是对Node变换的封装,下一节介绍。


Cocos2d-x 3.0final 终结者系列教程09-绘图节点Node中的Schedule,布布扣,bubuko.com

Cocos2d-x 3.0final 终结者系列教程09-绘图节点Node中的Schedule

标签:cocos2d-x   node 计划任务 schedule   

原文地址:http://blog.csdn.net/sdhjob/article/details/27497043

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!