当前位置:   article > 正文

Cocos2d-X官方Demo---1.ActionManager

Cocos2d-X官方Demo---1.ActionManager
  1. void HelloWorld::onEnter()
  2. {
  3. Scene::onEnter();
  4. Size visibleSize = Director::getInstance()->getVisibleSize();
  5. char*s_pathGrossini = "grossini.png";
  6. //1.CrashTest
  7. auto child = Sprite::create(s_pathGrossini);
  8. child->setPosition(visibleSize/2);
  9. addChild(child, 1, 1);
  10. //Sum of all action's duration is 1.5 second.
  11. child->runAction(RotateBy::create(1.5f, 90));
  12. child->runAction(Sequence::create(
  13. DelayTime::create(1.4f),
  14. FadeOut::create(1.1f),
  15. nullptr)
  16. );
  17. //After 1.5 second, self will be removed.
  18. child->runAction(Sequence::create(
  19. DelayTime::create(1.4f),
  20. CallFunc::create(/*CC_CALLBACK_0(CrashTest::removeThis, this)*/[this]() {
  21. auto child = getChildByTag(1);
  22. child->removeChild(child, true);
  23. }),nullptr));
  24. //2.LogicTest
  25. auto grossini = Sprite::create(s_pathGrossini);
  26. addChild(grossini, 0, 2);
  27. grossini->setPosition(visibleSize/2);
  28. grossini->runAction(Sequence::create(
  29. MoveBy::create(1, Vec2(150, 0)),
  30. CallFuncN::create(/*CC_CALLBACK_1(LogicTest::bugMe, this))*/[](Node*node) {
  31. node->stopAllActions(); //After this stop next action not working, if remove this stop everything is working
  32. node->runAction(ScaleTo::create(2, 2));
  33. }),nullptr));
  34. //3.PauseTest 暂停测试
  35. auto l = Label::createWithTTF("After 5 seconds grossini should move", "Thonburi.ttf", 16.0f);
  36. addChild(l);
  37. l->setPosition(visibleSize.width/2, visibleSize.height - 75);
  38. //
  39. // Also, this test MUST be done, after [super onEnter]
  40. //
  41. auto grossini = Sprite::create(s_pathGrossini);
  42. addChild(grossini, 0, 1);
  43. grossini->setPosition(visibleSize/2);
  44. auto action = MoveBy::create(1, Vec2(150, 0));
  45. auto director = Director::getInstance();
  46. director->getActionManager()->addAction(action, grossini, true);
  47. auto callback = (std::function<void(float)>)nullptr;
  48. callback = [this](float f) {
  49. unschedule("callback");
  50. auto node = getChildByTag(1);
  51. auto director = Director::getInstance();
  52. director->getActionManager()->resumeTarget(node);};
  53. schedule(/*CC_SCHEDULE_SELECTOR(PauseTest::unpause)*/callback, 3,"callback");
  54. //4.StopActionTest
  55. auto l = Label::createWithTTF("Should not crash", "Thonburi.ttf", 16.0f);
  56. addChild(l);
  57. l->setPosition(visibleSize.width/2, visibleSize.height - 75);
  58. auto pMove = MoveBy::create(2, Vec2(200, 0));
  59. auto pCallback = CallFunc::create(/*CC_CALLBACK_0(StopActionTest::stopAction*/[this]() {
  60. auto sprite = getChildByTag(2);
  61. sprite->stopActionByTag(1);
  62. });
  63. auto ret = MoveBy::create(1, Vec2(-200, 0));
  64. auto pSequence = Sequence::create(pMove, pCallback, ret, nullptr);
  65. pSequence->setTag(1);
  66. auto pChild = Sprite::create(s_pathGrossini);
  67. pChild->setPosition(visibleSize/2);
  68. addChild(pChild, 1, 2);
  69. pChild->runAction(pSequence);
  70. //5.StopAllActionsTest
  71. auto l = Label::createWithTTF("Should stop scale & move after 4 seconds but keep rotate", "Thonburi.ttf", 16.0f);
  72. addChild(l);
  73. l->setPosition(Vec2(visibleSize.width/2,visibleSize.height - 75));
  74. auto pMove1 = MoveBy::create(2, Vec2(200, 0));
  75. auto pMove2 = MoveBy::create(2, Vec2(-200, 0));
  76. auto pSequenceMove = Sequence::createWithTwoActions(pMove1, pMove2);
  77. auto pRepeatMove = RepeatForever::create(pSequenceMove);
  78. pRepeatMove->setTag(1);
  79. auto pScale1 = ScaleBy::create(2, 1.5f);
  80. auto pScale2 = ScaleBy::create(2, 1.0f / 1.5f);
  81. auto pSequenceScale = Sequence::createWithTwoActions(pScale1, pScale2);
  82. auto pRepeatScale = RepeatForever::create(pSequenceScale);
  83. pRepeatScale->setTag(1);
  84. auto pRotate = RotateBy::create(2, 360);
  85. auto pRepeatRotate = RepeatForever::create(pRotate);
  86. auto pChild = Sprite::create(s_pathGrossini);
  87. pChild->setPosition(visibleSize/2);
  88. addChild(pChild, 1, 2);
  89. pChild->runAction(pRepeatMove);
  90. pChild->runAction(pRepeatScale);
  91. pChild->runAction(pRepeatRotate);
  92. this->scheduleOnce(/*(SEL_SCHEDULE)&StopAllActionsTest::stopAction*/[this](float time) {
  93. auto sprite = getChildByTag(2);
  94. sprite->stopAllActionsByTag(1);
  95. }, 4,"callback");
  96. //6.ResumeTest
  97. auto l = Label::createWithTTF("Grossini only rotate/scale in 3 seconds", "Thonburi.ttf", 16.0f);
  98. addChild(l);
  99. l->setPosition(visibleSize.width/2, visibleSize.height - 75);
  100. auto pGrossini = Sprite::create(s_pathGrossini);
  101. addChild(pGrossini, 0, 1);
  102. pGrossini->setPosition(visibleSize/2);
  103. pGrossini->runAction(ScaleBy::create(2, 2));
  104. auto director = Director::getInstance();
  105. director->getActionManager()->pauseTarget(pGrossini);
  106. pGrossini->runAction(RotateBy::create(2, 360));
  107. auto callback = [this](float time) {
  108. unschedule("callback");
  109. auto pGrossini = getChildByTag(1);
  110. auto director = Director::getInstance();
  111. director->getActionManager()->resumeTarget(pGrossini);
  112. };
  113. this->schedule(/*CC_SCHEDULE_SELECTOR(ResumeTest::resumeGrossini)*/callback, 3.0f,"callback");
  114. //7.StopActionsByFlagsTest
  115. const unsigned int kMoveFlag = 0x01;
  116. const unsigned int kScaleFlag = 0x02;
  117. const unsigned int kRotateFlag = 0x04;
  118. const unsigned int kRepeatForeverFlag = 0x08; // You don't need this for the test, but it's for demonstration how to activate several flags on an action.
  119. auto l = Label::createWithTTF("Should stop scale & move after 4 seconds but keep rotate", "Thonburi.ttf", 16.0f);
  120. addChild(l);
  121. l->setPosition(Vec2(visibleSize.width/2, visibleSize.height - 75));
  122. auto pMove1 = MoveBy::create(2, Vec2(200, 0));
  123. auto pMove2 = MoveBy::create(2, Vec2(-200, 0));
  124. auto pSequenceMove = Sequence::createWithTwoActions(pMove1, pMove2);
  125. auto pRepeatMove = RepeatForever::create(pSequenceMove);
  126. pRepeatMove->setFlags(kMoveFlag | kRepeatForeverFlag);
  127. auto pScale1 = ScaleBy::create(2, 1.5f);
  128. auto pScale2 = ScaleBy::create(2, 1.0f / 1.5f);
  129. auto pSequenceScale = Sequence::createWithTwoActions(pScale1, pScale2);
  130. auto pRepeatScale = RepeatForever::create(pSequenceScale);
  131. pRepeatScale->setFlags(kScaleFlag | kRepeatForeverFlag);
  132. auto pRotate = RotateBy::create(2, 360);
  133. auto pRepeatRotate = RepeatForever::create(pRotate);
  134. pRepeatRotate->setFlags(kRotateFlag | kRepeatForeverFlag);
  135. auto pChild = Sprite::create(s_pathGrossini);
  136. pChild->setPosition(visibleSize/2);
  137. addChild(pChild, 1, 1);
  138. pChild->runAction(pRepeatMove);
  139. pChild->runAction(pRepeatScale);
  140. pChild->runAction(pRepeatRotate);
  141. this->scheduleOnce(/*(SEL_SCHEDULE)&StopActionsByFlagsTest::stopAction*/[this, kMoveFlag, kScaleFlag](float time) {
  142. auto sprite = getChildByTag(1);
  143. sprite->stopActionsByFlags(kMoveFlag | kScaleFlag);
  144. }, 4,"callback");
  145. //8. Issue14050Test
  146. class SpriteIssue14050 : public Sprite
  147. {
  148. public:
  149. SpriteIssue14050()
  150. {
  151. log("SpriteIssue14050::constructor");
  152. }
  153. virtual ~SpriteIssue14050()
  154. {
  155. log("SpriteIssue14050::destructor");
  156. }
  157. };
  158. auto sprite = new (std::nothrow) SpriteIssue14050;
  159. sprite->initWithFile("grossini.png");
  160. sprite->autorelease();
  161. addChild(sprite);
  162. auto move = MoveBy::create(2, Vec2(100, 100));
  163. auto rotate = RotateBy::create(2, 360);
  164. sprite->runAction(move);
  165. sprite->runAction(rotate);
  166. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/652368
推荐阅读
相关标签
  

闽ICP备14008679号