As mentioned above, the Node Action (Action), In the cocos2d-x, provides an ActionManger, used to manage some actions of Action, such as stop, pause, etc;
1. Stop all actions
In fact, the Node has some simple actions to manage, such as stopping all actions on the Node instance;
node->stopAllActions(); // node->runAction(ScaleTo::create(2, 2))
Call node-> stopAllActions (); all previous actions will be stopped, but it will affect subsequent actions, and subsequent ScaleTo actions will be executed normally;
Ii. delayed execution
In the cocos2d-x, there is no simple use of an interface to directly specify an action delay for a period of time after execution, but this is not a problem, the following is an example of a delay of 3 S to execute the action;
Auto grossini = Sprite: create (s_pathGrossini); addChild (grossini, 0, 98); // specify a tag grossini-> setPosition (VisibleRect: center () for sprite ()); auto action = MoveBy: create (1, Vec2 (150,0); auto director = Director: getInstance (); director-> getActionManager ()-> addAction (action, grossini, true); schedule (schedule_selector (PauseTest: unpause), 3); // The unpause method is executed every 3 seconds;
Unpause method implementation
Unschedule (schedule_selector (PauseTest: unpause); // stop the loop and run the unpause method auto node = getChildByTag (98); // obtain the sprite instance auto director = Director :: getInstance (); director-> getActionManager ()-> resumeTarget (node); // start the action
In the above example, the schedule loop update method is used to execute the task every 3 seconds. When the task is executed, the loop is canceled to implement delayed action execution;
3. Stop the action
Void StopActionTest: onEnter () {auto pMove = MoveBy: create (2, Vec2 (200, 0); auto pCallback = CallFunc: create (CC_CALLBACK_0 (StopActionTest :: stopAction, this); auto increment quence = Sequence: create (pMove, pCallback, NULL); Specify quence-> setTag (kTagSequence); // specify the tag auto pChild = Sprite for the action:: create (s_pathGrossini); pChild-> setPosition (VisibleRect: center (); addChild (pChild, 1, kTagGrossini); pChild-> runAction (Sequence: create (ScaleTo:: create (3.0f, 2.0f), NULL); pChild-> runAction (queue quence);} void StopActionTest: stopAction () // stop the action {auto sprite = getChildByTag (kTagGrossini); // get sprite-> stopActionByTag (kTagSequence) through the tag; // stop the action of the specified tag on the sprite}
To stop a Node, you can specify a tag for the action in advance. When you want to stop the Node, you can use stopActionByTag to stop it;
Iv. Resume action
void ResumeTest::onEnter(){ auto pGrossini = Sprite::create(s_pathGrossini); addChild(pGrossini, 0, kTagGrossini); pGrossini->setPosition(VisibleRect::center()); pGrossini->runAction(ScaleBy::create(2, 2)); auto director = Director::getInstance(); director->getActionManager()->pauseTarget(pGrossini);// @1 pGrossini->runAction(RotateBy::create(2, 360)); this->schedule(schedule_selector(ResumeTest::resumeGrossini), 3.0f);}void ResumeTest::resumeGrossini(float time){ this->unschedule(schedule_selector(ResumeTest::resumeGrossini)); auto pGrossini = getChildByTag(kTagGrossini); auto director = Director::getInstance(); director->getActionManager()->resumeTarget(pGrossini);}
@ 1: In the above Code, after the pauseTarget method is called, The runAction is called again. Will it execute this action first? Actually, this is not the case. Therefore, after pauseTarget, you must call resumeTarget to execute the uncompleted actions of the Node instance;
Complete!