Cocos2d-x ios game development first understanding (7) simple animation

Source: Internet
Author: User

Cocos2d-x ios game development first understanding (7) simple animation

In the previous section, frame animation is used to change the position, shape, and size of the genie to realize the corresponding action. This section mainly describes how to move the genie, various dazzling animations may also help you find some game scenarios.

The following code is used for analysis:

For clarity, it is best to comment out the code from the previous sections.

// Create a menu based on the previous knowledge

CCMenuItemFont * item = CCMenuItemFont: create ("Start game", this, menu_selector (MainScene: onMenuItem); // Click Event

// Add it to the menu bar

CCMenu * menu = CCMenu: create (item, NULL); // you can upload multiple

Menu-> setPosition (ccp (100,500 ));

This-> addChild (menu );

// Create an genie

Sprite = CCSprite: create ("Icon-72.png ");

Sprite-> setPosition (ccp (300,300 ));

This-> addChild (sprite );

Run:


Implement some animations in the onMenuItem function:

// 1. Hide the genie

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release/S/rLYvqvB6TwvcD4KPHA + release/Uy9DQtq + release = "http://www.2cto.com/uploadfile/Collfiles/20140819/2014081909105419.png" alt = "\">

The genie is hidden. There are a lot of animations to try.

CCHide * hide = CCHide: create (); // hide the genie

CCFadeTo * to = CCFadeTo: create (2, 0); // 0 ~ 255 fade-in and fade-out time: 2 s

CCMoveTo * toto = CCMoveTo: create (2, ccp (500,300); // It takes 2 s to move to a certain point.

CCMoveBy * toby = CCMoveBy: create (2, ccp (300, 0); // increase by x and y in 2 seconds

CCJumpBy * by = CCJumpBy: create (2, ccp (200, 0), 1); // jump to x within 2 seconds, y is (, 0) the position of each hop will jump 200 high, a total of 30 hops

Sprite-> runAction (hide); // run the animation

These are some basic animations that can not see the obvious effect, as long as sprite-> runAction (hide) changes the content in the brackets.

// 2. Implement a simple track

Trajectory image:


// Implement a trajectory

CCPointArray * pa = CCPointArray: create (5); // create a vertex Array

Pa-> addControlPoint (ccp (0, 0 ));

Pa-> addControlPoint (ccp (); // move to the right

Pa-> addControlPoint (ccp (100,-50); // move down

Pa-> addControlPoint (ccp (150,-50); // move to the right

Pa-> addControlPoint (ccp (); // move

Pa-> addControlPoint (ccp (, 0); // move to the right

CCCardinalSplineBy * spby = CCCardinalSplineBy: create (5, pa, 1); // move by track

Sprite-> runAction (spby); // run an animation

Run:


// 3. Implement a curve


Code:

// Use betiller to implement curve movement

CcBezierConfig bc;

Bc. controlPoint_1 = ccp (400,400); // Control Point 1

Bc. controlPoint_2 = ccp (800,200); // Control Point 2

Bc. endPosition = ccp (900,300); // end point

CCBezierTo * bezie = CCBezierTo: create (3, bc); // upload reference

Sprite-> runAction (bezie); // run an animation

Run:


Run it on your own to see the effect !!!

// 4. sequential execution of combined animation through CCSequence

// Use CCSequence to implement sequential execution of composite animations

CCMoveTo * tototo = CCMoveTo: create (2, ccp (900,300); // move

CCEaseSineIn * in = CCEaseSineIn: create (tototo)

CCBlink * blink = CCBlink: create (3, 10); // flashes an animation.

CCDelayTime * delay = CCDelayTime: create (2 );

CCSequence * sequence = CCSequence: create (in, delay, blink, NULL); // The composite animation moves first and then latencies finally flash

Sprite-> runAction (sequence); // run an animation

Run:


Run it on your own to see the effect !!!


// 5. Parallel Execution of composite animation through CCSpawn

// Execute the animation in parallel

CCMoveTo * totototo = CCMoveTo: create (2, ccp (900,300 ));

CCRotateBy * rotate = CCRotateBy: create (2,3600); // rotate 3600 degrees

CCSpawn * spawn = CCSpawn: create (totototo, rotate, NULL );

Sprite-> runAction (spawn );

Run:


Run it on your own to see the effect !!!

// 6. After the animation is executed, it can be returned.

Let's first look at the creation of the CCCallFunc class:

CCCallFunc * func = CCCallFunc: create (this, <# SEL_CallFunc selector #>) SEL_CallFunc Definition

Typedefvoid (CCObject: * SEL_CallFunc) (); from this we can see that SEL_CallFunc is a function pointer pointing to a function that returns void without function parameters.

The following defines a function:

. H file:

Class MainScene: publicCCLayer {

Private:

Void onCallFun (); // call back the function after the animation is executed.

. Cpp file function implementation:

VoidMainScene: onCallFun ()

{

CCLOG ("action over ");

}

Only one print ..

The specific implementation code is as follows:

// Execute the animation in parallel

CCMoveTo * totototo = CCMoveTo: create (2, ccp (900,300 ));

CCRotateBy * rotate = CCRotateBy: create (2,3600); // rotate 3600 degrees

CCSpawn * spawn = CCSpawn: create (totototo, rotate, NULL );

// Sprite-> runAction (spawn );

// Return the animation after it is executed.

CCCallFunc * func = CCCallFunc: create (this, callfunc_selector (MainScene: onCallFun ));

CCSequence * seq = CCSequence: create (spawn, func, NULL); // Add to queue

Sprite-> runAction (seq );

Run:


After the animation is executed, the action over is printed...



Related Article

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.