Cocos2dx 3.1 learning from scratch (5) -- animation

Source: Internet
Author: User
Animation is the most important part of the game. This article is just a preliminary study of the use of most animations. There are no principles, but there are many examples. If you are not familiar with it, you need to practice it. In particular, the combination of spawn and sequence, when to use spawn, what to use sequence, and how to nest each other between them. Use your brain cells to torture these functions! Make as many brilliant combinations as possible! All animation methods are in node. Its sub-classes include layer, Sprite, and menuitem. Runaction start animation stopaction stop animation stopactionbytag stop animation according to tag ......
Animation PrincipleCreate an animation, and execute runaction where you want to execute the action to add an animation to it. The animation principle is not studied in depth for the moment, and the project will be improved later.
Common animations Fade (In, out) Move (to,) Jump (to,)
Eg: how to achieve a second-level hop in a game that allows a certain genie to randomly move to a certain point on the screen (pay attention to average speed)
Blink flashing The sequence combination sequence is executed in sequence. Note that the parameter ends with null. Delaytime delayed execution
Eg: design an animated sequence. After the translation reaches 200, the system will jump back four times after a delay of 1 second.
Rotate (to, by) rotates around anchorpoint Scale (to, by) Scaling animation, S is to zoom in and out ratio The spawn combines parallel animation and the action is executed simultaneously. Note that the parameter ends with null.
Eg:Design an animation to fill the screen after the background is rotated and zoomed. design an animation sequence, move 300 pixels forward, translate 100, and then jump 100.
Animation callbackCallfunc and sequence use callfuncn in combination
Eg: Four genie moved to the four corners of the screen at the center.
// 4 genie in? Are the four positions successively moved to the screen? Void secondaryjump: foursprites (cocos2d: ref * ref) {auto act = sequence: Create (moveTo: Create (1, point (10, 10), callfuncn :: create ([=] (node * node) {SP5-> runaction (sequence: Create (moveTo: Create (1, point (10,630), callfuncn :: create ([=] (node * node) {SP6-> runaction (sequence: Create (moveTo: Create (1, point (950,630), callfuncn :: create ([=] (node * node) {sp7-> runaction (sequence: Create (moveTo: Create (1, point (950, 10 )), nullptr);}), nullptr); SP4-> runaction (ACT );}


Animation callback and sound effects
// Auto callfunc = callfuncn: Create ([=] (node * node) {node-> setvisible (false) ;}); Auto act = sequence :: create (spawn: Create (rotateto: Create (2,720), moveBy: Create (2, point (100, 0), nullptr), moveBy: Create (1, point (100, 0), jumpby: Create (0.2f, point (0, 0), 100, 1), callfunc, callfuncn :: create ([=] (node * node) {cocosdenshion: simpleaudioengine: sharedengine ()-> playbackgroundmusic ("music/game_music.wav", true) ;}), nullptr ); SP2-> runaction (Act );


Other animationsLoop animation repeat and rpeatforever // implement the jitter of a menu button. For example, if the input account or password is incorrect, the input box jitter error is returned. In the callback function of menuitem, this is actually repeated six times in the upper left, upper right, and lower right corner of the left:
void SecondaryJump:: Jump (cocos2d :: Ref * ref ){    MenuItemFont * item = (MenuItemFont *) ref;    auto repeatAct = Repeat :: create( Sequence ::create ( MoveBy:: create (0.01f, Point (3, 3)), MoveBy ::create (0.01f, Point(-3, -3)), MoveBy ::create (0.01f, Point(3, -3)), MoveBy ::create (0.01f, Point(-3, 3)), nullptr ), 6);    item-> runAction (repeatAct );}

Frame Animation animate beiser Curve
Void frameanimation: morning_0623 (cocos2d: ref * ref) {auto animation = Animation: Create (); For (INT I = 2; I <= 6; I ++) {string filename = stringutils: Format ("CoC/characters_lowres/character d.0.png", I); animation-> addspriteframewithfilename (filename. getcstring ();} animation-> setdelayperunit (0.05f); animation-> setloops (-1); Auto animate = Animate: Create (animation); Auto Parr = pointarray :: create (5); Parr-> addcontrolpoint (point (100, 0); Parr-> addcontrolpoint (point (100,200); Parr-> addcontrolpoint (point (300,200 )); parr-> addcontrolpoint (point (300,400); auto move = cardinalsplineby: Create (5, Parr, 1); Auto act = spawn: Create (animate, move, nullptr); ccbezierconfig beziercon; beziercon. controlpoint_1 = ccpointmake (200,150); // Control Point 1 beziercon. controlpoint_2 = ccpointmake (200,160); // Control Point 2 beziercon. endposition = ccpointmake (540,100); // end position SP-> runaction (spawn: Create (Act, bezierby: Create (3, beziercon), nullptr ));}

Eg:
There are 10 genie randomly moving on the screen, and the Genie have the corresponding random blood volume. After the touch, the genie with less blood volume disappears, and only the last genie is left. (The genie is encapsulated into the enemy class) code analysis: three classes in my code: spritehw, spritehwt, and animationhw. The difficulty of this question lies in the deletion after the genie collision detection. For details, refer to update of animationhw. Using the vector of C ++ itself, instead of deleting it during traversal, it marks the deletion. After the collision detection, traverse the vector again to delete the deleted elements. For the question about the random location of the genie, the random number will be the same every time you run Rand. Here we add a Random Seed to it, as long as we add a srand (unsigned) in Init) time (null); random numbers are different each time.




Currently, cocos2dx does not support Chinese characters. I have not found good open-source code for Unicode and other transcoding tasks. I do not want to write code in disorder due to limited capabilities.

Code compressed package: http://download.csdn.net/detail/hanbingfengying/7548925code svnsynchronous address (if someday No, forgive me, may not renew): https://www.svnchina.com/svn/lanou

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.