Cocos2d notes (3) various actions in cocos2d

Source: Internet
Author: User

(This section learned from "Zhiyi cocos2d-iphone development tutorial")

 

Generally, the following method is used to call an action: 


 

 
// Create action ID actionto = [ccmoveto actionwithduration: 2 position: CCP (S. Width-40, S. Height-40)]; // use action. (Description: Tamara is a ccsprite .) [Tamara runaction: actionto];


 


Instantaneous action

As the name suggests. An instantaneous action is an action completed immediately without time. The common base class of an instantaneous action is instantaction.

 

Placement-place

The effect is similar to node. Position = CCP (x, y ). It is implemented as an action to form a continuous action with other actions.

Example:

-(Void) onplacemenue :( ID) sender {cgsize S = [[ccdirector shareddire] winsize]; cgpoint P = CCP (ccrandom_0_1 () * s. width, ccrandom_0_1 () * s. height); [sprite runaction: [ccplace actionwithposition: p];}

 

Hide-hide

The effect is similar to [node setvisible: No].

Example:

 
-(Void) onhidemenue :( ID) sender {[sprite runaction: [cchide action];}

 

Show

The effect is similar to [node setvisible: Yes].

Example:

 
-(Void) onshowmenue :( ID) sender {[sprite runaction: [ccshow action];}

Visible switch-togglevisibility 

CodeAs follows:

-(Void) ontogglemenue :( ID) sender {[sprite runaction: [cctogglevisibility Action];}

Delayed action

A delayed action takes some time to complete the action. Therefore, actionwithduration is the first parameter for execution of delayed actions. The common base class of delayed actions is ccintervalaction.

Function naming rules:

* ** To: it indicates the motion to the specified position.

* ** By: it means that the motion is directed to the specified increment X and Y. (X and y can be negative values)

Move to-ccmoveto

Mobile-Ccmoveby

Jump to-ccjumpto

Set the destination position and the hop height and number of times.

Skip-ccjumpby

Set the destination position and the hop height and number of times.

Besell-ccbezierby

Support for three besell curves: P0-start point, P1-start point tangent direction, P2-end point tangent direction, and P3-end point.

First, set the besuppliers parameter, and then execute.

Zoom in to-ccscaleto

Set the magnification.

Zoom in-ccscaleby

Rotate to-ccrotateto

Rotate-ccrotateby

Flashing-ccblink

Set the number of flashes

Tone changed to-cctintto

Tone conversion-cctintby

Darken to-ccfadeto

From non-Brightening-ccfadein

From bright to non-ccfadeout


Combined Action

Combine the above basic actions in a certain order to form a coherent set of actions. Combined Actions include the following types:

Sequence-ccsequence

The main function is to arrange several actions in line order and then execute them one by one in order.

-(Void) onsequence :( ID) sender {cgsize S = [[ccdirector shareddire] winsize]; cgpoint P = CCP (S. width/2, 50); // create 5 Action IDs ac0 = [sprite runaction: [ccplace actionwithposition: p]; Id AC1 = [ccmoveto actionwithduration: 2 position: CCP (S. width-50, S. height-50)]; Id ac2 = [ccjumpto actionwithduration: 2 position: CCP (150, 50) Height: 30 jumps: 5]; Id AC3 = [ccblink actionwithduration: 2 blinks: 3]; Id ac4 = [Cctintby actionwithduration: 0.5 RED: 0 Green: 255 Blue: 255]; // combine five actions into a sequence. Do not forget to end with nil. [Sprite runaction: [ccsequence actions: ac0, AC1, ac2, AC3, ac4, ac0, nil];}

Synchronization-spawn

It is used to concurrently execute several actions, but all actions must be executed simultaneously. For example, mobile flip, color change, and size change.

Note that the final completion time of synchronous execution is determined by the time spent in the basic action.

-(Void) onspawn :( ID) sender {cgsize S = [[ccdirector shareddire] winsize]; cgpoint P = CCP (S. width/2, 50); Sprite. rotation = 0; [sprite setposition: p]; // create four actions that require parallel operation to ensure that the actions can be combined. 2-2-(1 + 1) ID AC1 = [ccmoveto actionwithduration: 2 position: CCP (S. width-50, S. height-50)]; Id ac2 = [ccrotateto actionwithduration: 2 angle: 180]; Id AC3 = [ccscaleto actionwithduration: 1 scale: 4]; Id ac4 = [ccscaleby actionwithduration: 1 scale: 0.5]; Id seq = [ccsequence actions: Ac3, ac4, nil]; // synchronous and combined actions form a continuous new action. [Sprite runaction: [ccspawn actions: AC1, ac2, seq, nil];}

 

Repeate

The following is an example of repeated actions:

-(Void) onrepeat :( ID) sender {cgsize S = [[ccdirector shareddire] winsize]; cgpoint P = CCP (S. width/2, 50); Sprite. rotation = 0; [sprite setposition: p]; // create an action sequence ID AC1 = [ccmoveto actionwithduration: 2 position: CCP (S. width-50, S. height-50)]; Id ac2 = [ccjumpby actionwithduration: 2 position: CCP (-400,-200) Height: 30 jumps: 5]; Id AC3 = [ccjumpby actionwithduration: 2 position: CCP (S. width/2, 0) He Ight: 20 jumps: 3]; Id seq = [ccsequence actions: AC1, ac2, AC3, nil]; // re-run the preceding sequence three times. [Sprite runaction: [ccrepeat actionwithaction: seq times: 3];}

 

Reactionary Work-reverse

Reactionary action refers to reverse (reverse) execution of an action, supporting the reactionary action sequence for the action sequence. Reactionary action is not a special class, but an interface introduced by ccfiniteaction. Not all classes support reactionary actions. xxxxto classes generally do not support reactionary actions. xxxxby classes generally support reactionary actions. Example:

-(Void) onreverse :( ID) sender {cgsize S = [[ccdirector shareddire] winsize]; cgpoint P = CCP (S. width/2, 50); Sprite. rotation = 0; [sprite setposition: p]; Id AC1 = [ccmoveby actionwithduration: 2 position: CCP (190,220)]; // create a sequence for reverse execution. Id ac2 = [AC1 reverse]; [sprite runaction: [ccrepeat actionwithaction: [ccsequence actions: AC1, ac2, nil] times: 2];}

 

Animation-Animation

Animation allows the genie to continuously execute an image to form a simulated motion effect: the animation status during walking and the fighting status.

 
-(Void) onanimation :( ID) sender {ccanimation * animation = [atlasanimation animationwithname: @ "flight" Delay: 0.2f]; // content definition of each frame. For (INT I = 0; I <3; I ++) {int x = I % 3; [animation addframewithrect: cgrectmake (x * 32, 0, 31,30)];} // execute the upload image effect ID action = [ccanimate actionwithanimation: animation]; [sprite runaction: [ccrepeat actionwithaction: Action times: 10];}

 

Unlimited repeatforever

Repeatforever is derived directly from the action class, so it cannot be involved in sequence and synchronization, and it cannot be executed in reverse direction. This class is used to execute an action or action sequence indefinitely until it is stopped.

-(Void) onrepeatforever :( ID) sender {cgsize S = [[Director shareddire] winsize]; cgpoint P = CCP (100, 50 ); // ccanimation * animation = [ccanimation animationwithname: @ "flight" Delay: 0.1f]; for (INT I = 0; I <3; I ++) {int x = I % 3; [animation addframewithrect: cgrectmake (x * 32, 0, 31,30)];} ID action = [ccanimate actionwithanimation: animation]; // use this animation as the genie's intrinsic animation and keep running. [Sprite runaction: [repeatforever actionwithaction: Action]; // create a second continuous indefinite action sequence. Overlay the two to form a complete effect. Ccbezierconfig bezr; Sprite. rotation = 0; [sprite setposition: p]; bezr. startposition = CCP (0, 0); bezr. controlpoint_1 = CCP (0, S. height/2); bezr. controlpoint_2 = CCP (300,-s. height/2); bezr. endposition = CCP (300,100); Id ac10 = [ccbezierby actionwithduration: 3 besuppliers: besuppliers]; Id ac11 = [cctintby actionwithduration: 0.5 RED: 0 Green: 255 Blue: 255]; id AC1 = [ccspawn actions: ac10, [Repeat actionwit Haction: ac11 times: 4], nil]; Id ac2 = [ccspawn actions: [ac10 reverse], [ccrepeat actionwithaction: ac11 times: 4], nil]; // The second consecutive operation period. [Sprite runaction: [ccrepeatforever actionwithaction: [ccsequence actions: AC1, ac2, nil];}

 

Speed Change

The basic and composite actions change the movements and animation effects of the genie, but the speed of such changes remains unchanged, through the cceaseaction class and ccspeed class, we can easily modify the speed at which the genie executes the action: From fast to slow or from slow to fast

Easein is slow to fast.

Easeout from fast to slow

Easeinout starts from slow to fast and then from fast to slow.

Easesinein from slow to fast

Easesineout from fast to slow

Easesineinout starts from slow to fast and then from fast to slow.

Easeexponentialin is slow and fast.

Easeexponentialout is fast to slow.

Easeexponentialinout is slow from extremely fast to fast.

Speed: manually set the speed. You can also use setspeed to continuously adjust the speed.

 

Scaling action

We have mastered the execution of a variety of actions, but also according to the speed of the modification of the Action execution time, Cocos2D-iPhone also provides an extension of the existing action to achieve a variety of flexible effects.

Delayed action-Delay

Add a time interval to the action sequence:

-(Void) ondelay :( ID) sender {ID AC1 = [ccmoveby actionwithduration: 2 position: CCP (200,200)]; Id ac2 = [AC1 reverse]; // implement a waiting interval [spriterunaction: [sequenceactions: AC1, [delaytime actionwithduration: 1], ac2, nil];}

 

Function call Function

Call a function in the middle or end of the action sequence to execute any task that needs to be executed: Action, status modification, etc. The Code is as follows:

 
-(Void) oncallfunc :( ID) sender {ID AC1 = [ccmoveby actionwithduration: 2 position: CCP (200,200)]; Id ac2 = [AC1 reverse]; id ACF = [cccallfunc actionwithtarget: Self selector: @ selector (callback1)]; [sprite runaction: [ccsequence actions: AC1, ACF, ac2, nil];}

The corresponding function is: (perform another action to achieve arbitrary expansion and connection of the action and action sequence)

 
-(Void) callback1 {[sprite runaction: [cctintby actionwithduration: 0.5 RED: 255 Green: 0 Blue: 255];}

When a user-defined function is called with object parameters, the current object is passed.

-(Void) oncallfuncn :( ID) sender {ID AC1 = [ccmoveby actionwithduration: 2 position: CCP (200,200)]; Id ac2 = [AC1 reverse]; id ACF = [callfuncn actionwithtarget: Self selector: @ selector (callback2 :)]; [sprite runaction: [ccsequence actions: AC1, ACF, ac2, nil];}

Corresponding User-Defined Function: (here, we use this object directly)

 
-(Void) callback2 :( ID) sender {[Sender runaction: [cctintby actionwithduration: 1 RED: 255 Green: 0 Blue: 255];}

When a user-defined function is called with an object or data parameter, the current object and a constant (or a pointer) are passed ).

 
-(Void) oncallfuncnd :( ID) sender {ID AC1 = [ccmoveby actionwithduration: 2 position: CCP (200,200)]; Id ac2 = [AC1 reverse]; id ACF = [cccallfuncnd actionwithtarget: Self selector: @ selector (callback3: Data :) data :( void *) 2]; [sprite runaction: [ccsequence actions: AC1, ACF, ac2, nil];}

For the corresponding user-defined functions, we use the passed objects and data:

-(Void) callback3 :( ID) sender data :( void *) Data {[Sender runaction: [cctintby actionwithduration :( nsinteger) Data RED: 255 Green: 0 Blue: 255];}

 

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.