Action Management in Cocos2d-x

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.