Cocos2d-x how to control the action speed, cocos2d-x control action

Source: Internet
Author: User

Cocos2d-x how to control the action speed, cocos2d-x control action
The basic actions and composite actions change the movements and animation effects of the genie. But this changes the speed at a uniform and linear speed. With ActionEase, Its Derived classes, and the Speed class, we can make the genie move at a non-uniform or non-linear Speed, which makes the effect more realistic.

Shows the category diagram of ActionEase.




The following is an example of how to use speed control in these actions. This is an operation menu scenario. You can select a menu to enter the action scenario, in the action scenario, click the Go button to execute the selected action effect. Click the Back button to return to the menu scenario.


Next let's take a look at the specific program code. First, let's take a look at the HelloWorldScene. h file. Its code is as follows:

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"#include "MyActionScene.h" typedef enum                                                                                                                                         ①{    kEaseIn = 1   ,kEaseOut   ,kEaseInOut   ,kEaseSineIn   ,kEaseSineOut   ,kEaseSineInOut   ,kEaseExponentialIn   ,kEaseExponentialOut   ,kEaseExponentialInOut   ,kSpeed   } ActionTypes;                                                                                                                             ②  class HelloWorld : public cocos2d::Layer{public:   static cocos2d::Scene* createScene();   virtual bool init();    void OnClickMenu(cocos2d::Ref* pSender);      CREATE_FUNC(HelloWorld);}; #endif // __HELLOWORLD_SCENE_H__

Code ① ~ ② Define an enumeration type ActionTypes. 10 constants are defined in the enumeration type ActionTypes. These 10 constants correspond to 10 menu items.

The HelloWorldScene implementation code HelloWorldScene. ccp file. Its main code is as follows:

bool HelloWorld::init(){    if( !Layer::init() )    {         returnfalse;    }     SizevisibleSize = Director::getInstance()->getVisibleSize();    Pointorigin = Director::getInstance()->getVisibleOrigin();     autobg = Sprite::create("background.png");    bg->setPosition(Point(visibleSize.width/2,visibleSize.height /2));    this->addChild(bg);     autopItmLabel1 = Label::createWithBMFont("fonts/fnt2.fnt","EaseIn");    autopItmMenu1 = MenuItemLabel::create(pItmLabel1,              CC_CALLBACK_1(HelloWorld::OnClickMenu, this));    pItmMenu1->setTag(kEaseIn);     autopItmLabel2 = Label::createWithBMFont("fonts/fnt2.fnt","EaseOut");    autopItmMenu2 = MenuItemLabel::create(pItmLabel2,             CC_CALLBACK_1(HelloWorld::OnClickMenu,this));    pItmMenu2->setTag(kEaseOut);     autopItmLabel3 = Label::createWithBMFont("fonts/fnt2.fnt","EaseInOut");    autopItmMenu3 = MenuItemLabel::create(pItmLabel3,             CC_CALLBACK_1(HelloWorld::OnClickMenu,this));    pItmMenu3->setTag(kEaseInOut);     autopItmLabel4 = Label::createWithBMFont("fonts/fnt2.fnt","EaseSineIn");    autopItmMenu4 = MenuItemLabel::create(pItmLabel4,             CC_CALLBACK_1(HelloWorld::OnClickMenu,this));    pItmMenu4->setTag(kEaseSineIn);     autopItmLabel5 = Label::createWithBMFont("fonts/fnt2.fnt", "EaseSineOut");    autopItmMenu5 = MenuItemLabel::create(pItmLabel5,             CC_CALLBACK_1(HelloWorld::OnClickMenu,this));    pItmMenu5->setTag(kEaseSineOut);     autopItmLabel6 = Label::createWithBMFont("fonts/fnt2.fnt","EaseSineInOut");    autopItmMenu6 = MenuItemSprite::create(pItmLabel6,             CC_CALLBACK_1(HelloWorld::OnClickMenu,this));    pItmMenu6->setTag(kEaseSineInOut);     autopItmLabel7 = Label::createWithBMFont("fonts/fnt2.fnt","EaseExponentialIn");    autopItmMenu7 = MenuItemSprite::create(pItmLabel7,             CC_CALLBACK_1(HelloWorld::OnClickMenu,this));    pItmMenu7->setTag(kEaseExponentialIn);     autopItmLabel8 = Label::createWithBMFont("fonts/fnt2.fnt","EaseExponentialOut");    autopItmMenu8 = MenuItemLabel::create(pItmLabel8,             CC_CALLBACK_1(HelloWorld::OnClickMenu,this));    pItmMenu8->setTag(kEaseExponentialOut);     autopItmLabel9 = Label::createWithBMFont("fonts/fnt2.fnt","EaseExponentialInOut");    autopItmMenu9 = MenuItemLabel::create(pItmLabel9,             CC_CALLBACK_1(HelloWorld::OnClickMenu,this));    pItmMenu9->setTag(kEaseExponentialInOut);     autopItmLabel10 = Label::createWithBMFont("fonts/fnt2.fnt","Speed");    autopItmMenu10 = MenuItemLabel::create(pItmLabel10,             CC_CALLBACK_1(HelloWorld::OnClickMenu,this));    pItmMenu10->setTag(kSpeed);     automn = Menu::create(pItmMenu1,pItmMenu2,pItmMenu3,pItmMenu4,pItmMenu5,         pItmMenu6,pItmMenu7,pItmMenu8,pItmMenu9,pItmMenu10,NULL);     mn->alignItemsInColumns(2,2, 2, 2, 2, NULL); this->addChild(mn);     returntrue;} void HelloWorld::OnClickMenu(Ref* pSender){    MenuItem*nmitem = (MenuItem*)pSender;     auto  sc = Scene::create();    auto  layer = MyAction::create();    layer->setTag(nmitem->getTag());     sc->addChild(layer);     autoreScene = TransitionSlideInR::create(1.0f, sc);    Director::getInstance()->replaceScene(reScene);}

You are familiar with the appeal code, so we will not introduce it here. Next let's take a look at the next scenario MyActionScene, its MyActionScene. ccp. Its main code is as follows:

void MyAction::goMenu(Ref* pSender){      log("Tag = %i",this->getTag());   FiniteTimeAction * ac1 = (FiniteTimeAction *)MoveBy::create(2,Point(200, 0));   FiniteTimeAction * ac2 = ((FiniteTimeAction *)ac1)->reverse();      ActionInterval * ac = Sequence::create(ac1, ac2, NULL);      switch (this->getTag()) {       case kEaseIn:           sprite->runAction(EaseIn::create(ac, 3));                                                                        ①            break;       case kEaseOut:           sprite->runAction(EaseOut::create(ac, 3));                                                            ②            break;       case kEaseInOut:            sprite->runAction(EaseInOut::create(ac,3));                                                         ③            break;       case kEaseSineIn:           sprite->runAction(EaseSineIn::create(ac));                                                           ④            break;       case kEaseSineOut:           sprite->runAction(EaseSineOut::create(ac));                                                                  ⑤            break;       case kEaseSineInOut:           sprite->runAction(EaseSineInOut::create(ac));                                                              ⑥            break;       case kEaseExponentialIn:           sprite->runAction(EaseExponentialIn::create(ac));                                                       ⑦            break;       case kEaseExponentialOut:           sprite->runAction(EaseExponentialOut::create(ac));                                                     ⑧            break;       case kEaseExponentialInOut:           sprite->runAction(EaseExponentialInOut::create(ac));                                        ⑨            break;       case kSpeed:           sprite->runAction(Speed::create(ac, (CCRANDOM_0_1() * 5)));                                  ⑩            break;   }}

The first line of code sprite-> runAction (EaseIn: create (ac, 3) is a speed 3 times from slow to fast. Code 2 sprite-> runAction (EaseOut: create (ac, 3) is a speed 3 times from fast to slow. ③ Code sprite-> runAction (EaseInOut: create (ac, 3) is a speed of three times from slow to fast and then from fast to slow.

Code ④ sprite-> runAction (EaseSineIn: create (ac) is a sine transformation speed from slow to fast. The 5th code sprite-> runAction (EaseSineOut: create (ac) uses the sine transform speed from fast to slow. Code 6 sprite-> runAction (EaseOut: create (ac, 3) uses the sine transform speed from slow to fast and then from fast to slow.

Code 7 sprite-> runAction (EaseExponentialIn: create (ac) uses exponential transformation speed from slow to fast. The vertex code sprite-> runAction (EaseExponentialOut: create (ac) uses the exponential transformation speed from fast to slow. The Nth code sprite-> runAction (EaseExponentialInOut: create (ac) uses the exponential transformation speed from slow to fast and then from fast to slow.

The Nth code sprite-> runAction (Speed: create (ac, (CCRANDOM_0_1 () * 5) randomly sets the conversion Speed.



More content please pay attention to the Cocos2d-x series of books "Cocos2d-x practice (Volume I): C ++ development" book exchange discussion site: http://www.c Ocoagame.netWelcome to cocos2d-x Technology Discussion Group: 257760386, 327403678

 

 

 

 


Cocos2d-x Sprite Action problems

# Define THETAG 123/* set this number as needed */
Initialization action:
CCAction * pAction = the action you want to execute;
PAction-> setTag (THETAG );
PSprite-> runAction (pAction); // the action you performed
Acceleration:
PSprite-> stopActionByTag (THETAG );
PAction = CCSpeed: actionWithAction (
The action you want to perform/* Note that the time must be set to 20 seconds, because the acceleration actually takes 10 seconds to execute 20 seconds */,
2/* if you want to slow down, it will be 0.7 */);
PAction-> setTag (THETAG );
PSprite-> runAction (CCSequence: actions (
PAction,
CCCallFunc: actionWithTarget (pSprite, callfunc_selector (callback function )),
NULL

));

The following is the internal callback function: // This function is a member function of the genie that executes the action.
StopActionByTag (THETAG );
CCAction * pAction = the action you want to execute;
PAction-> setTag (THETAG );
RunAction (pAction );

Cocos2d-x, how to implement the genie not responding to other touch events before an action ends

This problem is common, that is, you click it once and let it jump. Wait until it falls down, click again, and then jump again. Clicking during the animation will not jump.
Generally, in game development, we are used to using a bool en variable for control. You should first click the event, that is, set en to false when the genie runAction, in addition, jumpBy is recommended for Skip animations. A callback is added to this callback to set en to true when the animation ends.
We recommend that you take a look at damo. Thank you ~ Wish you growth.

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.