Some of the previous articles about COCOS2DX are based on static things like controls, scenes, etc. In fact, COCOS2DX provides a large number of action implementation methods, moving a genie is not so troublesome, call the method directly, the only difficulty is how to combine multiple actions, the following example to illustrate the problem.
Make the following example:
There is a self-bringing button sprite that has been played down, first flashing 3, then moving up, waiting for 1 seconds to bounce down, then slowly falling.
This effect is very great, if you want to use flash and other languages to do not know how many keyframes and code to waste, but COCOS2DX implementation is very simple.
The production process is as follows:
First or new in the CPP language written in the name of the Moveaction COCOS2DX project, open the Proj.win32 in the HelloCpp.sln began to write the program, this step has been mentioned in the previous article many times, will not be able to see "COCOS2DX" Download, install and configure COCOS2DX 2.x under the Windows platform to create your own HelloWorld "(Click to open link).
After the AppDelegate.cpp is switched off, the HelloWorldScene.cpp is modified directly to the following, with emphasis on the bool Helloworld::init () function in it:
#include "HelloWorldScene.h" USING_NS_CC; ccscene* Helloworld::scene () {//' scene ' is a autorelease objectccscene *scene = Ccscene::create ();//' layer ' is an Autore Lease Objecthelloworld *layer = Helloworld::create ();//Add layer as a child to scenescene->addchild (layer);//Return T He scenereturn scene;} On "Init" need to initialize your Instancebool helloworld::init () {//Get screen size, location information, etc. ccsize visiblesize = Ccdirec Tor::shareddirector ()->getvisiblesize (); Set the sprite and add the Genie to the center of the stage Ccsprite *sprite=ccsprite::create ("Closeselected.png"); Sprite->setposition (CCP ( VISIBLESIZE.WIDTH/2,VISIBLESIZE.HEIGHT/2)); This->addchild (sprite);//Set action//action 1ccfinitetimeaction* action1= Ccblink::create (1.0f,3); Flashes three times in one second//action 2ccfinitetimeaction* action2_1=ccmoveby::create (1.0F,CCP (0,100)); Lift 100pxccfinitetimeaction* action2_2=ccmoveby::create (1.0F,CCP (0,0)) within 1 seconds; Wait 1 seconds ccfinitetimeaction* action2_3=ccjumpby::create (0.75F,CCP (0,-50), 20, 3); In 0.75 seconds, the first jump up to 20px and then a total of 50px, the action repeats 3 times, will only end up moving down 90pxCCFiniteTimeaction* action2=ccsequence::create (Action2_1, Action2_2, Action2_3, NULL); Combining the above actions into the action sequence action2//action 3float action3_time=5.0f; ccfinitetimeaction* action3_1=ccrotateby::create (action3_time,-3600);//5 seconds in counterclockwise direction-3600 degrees ccfinitetimeaction* Action3_2=ccmoveto::create (ACTION3_TIME,CCP (Visiblesize.width/2,sprite->getcontentsize (). height));// Move to the bottom of the screen in 5 Seconds ccfinitetimeaction* action3=ccspawn::create (action3_1,action3_2,null); Combine the above two actions, that is, two actions simultaneously//merge the above actions, and apply to the Elf ccfinitetimeaction* sequence=ccsequence::create (Action1,action2,action3, NULL); sprite->runaction (sequence); return true;} void Helloworld::menuclosecallback (ccobject* psender) {#if (Cc_target_platform = = CC_PLATFORM_WINRT) | | (Cc_target_platform = = CC_PLATFORM_WP8) Ccmessagebox ("You pressed the Close button. Windows Store Apps do not implement a close button. "," "Alert"); #elseCCDirector:: Shareddirector ()->end (); #if (cc_ Target_platform = = Cc_platform_ios) exit (0); #endif #endif}
Among them, there are a few points to be aware of.
(1) Ccblink, Ccmoveby, Ccjumpby, Ccrotateby, Ccmoveto are some basic actions, ccsequence and Ccspawn are the conformity of these actions.
(2) The difference between Ccmoveby and Ccmoveto is the difference between "move" and "Move to". Ccmoveby is the coordinates of "moved" (X_num,y_num) in the (x, y) direction, Ccmoveto is moved to (x, y).
(3) In-situ pause for 1 seconds, there is no need to use what timer and other complex things, is to require elves in 1 seconds in the direction of (x, y) move 0px
(4) How to need to finish an action, then do an action, you can not use ccsequence. For example the above Code of Action 1, first rise 100px and then stop 1 seconds last down jump, if not using ccfinitetimeaction* action2=ccsequence::create (Action2_1, Action2_2, action2_ 3, NULL); By merging them, the genie will not know what direction to move.
(5) The difference between ccsequence and Ccspawn, Ccsequence is done 1 action to do 1 action, Ccspawn is all the action, together, so the action is ccspawn, its execution time should be consistent.
(6) Both Ccsequence and Ccspawn, the combination of action, all need to pay attention to, the last action must be null, otherwise it will appear the following situation, can not be compiled, I do not know what is going on:
(7) All basic movements Ccblink, Ccmoveby, Ccjumpby, Ccrotateby, Ccmoveto and movements of the Assembly Ccsequence, Ccspawn are ccfinitetimeaction, Although the existence of Ccfinitetimeaction is a subclass of ccaction relationships, such as:
However, as in Java ArrayList and List interchange, the ccfinitetimeaction replaced with Ccaction, although arguably the subclass can be replaced by its more abstract parent class, however, I do not know why, after the replacement is not compiled, Appear as the situation, so play cocos2dx action, or good use ccfinitetimeaction:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"COCOS2DX" basic action, action sequence and action merging