Cocos2d-x 3.0final 终结者系列教程09-漆节点Node中间Schedule

怎么做HelloWorld工程HelloWorld文字实现它自己主动运动?

有的童鞋会想到使用线程。不断的变化Label的Position,

不要那样做,因为Cocos2d-x在主线程只能被改变Node信息。这都是由于node全的。假设我们的场景移除了node

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

用法非常easy

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转换软件包,以下部分介绍。


版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/gcczhongduan/p/4612561.html