Various actions in cocos2d

Source: Internet
Author: User
Usually call the method of an action: // create action ID actionto = [ccmoveto actionwithduration: 2 position: CCP (S. width-40, S. height-40)]; // use the 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. The placement-place 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 shareddirector] winsize]; cgpoint P = CCP (ccrandom_0_1 () * s. width, ccrandom_0_1 () * s. height); [sprite runaction: [ccplace actionwithposition: p];} Hide-hide effect similar to [node setvisible: No]. example:-(void) onhidemenue :( ID) sender {[sprite runaction: [cchide action];} display-show effect similar to [node setvisible: Yes]. example:-(void) onshowmenue :( ID) sender {[Sprite Runaction: [ccshow action];} the code for togglevisibility is as follows:-(void) ontogglemenue :( ID) sender {[sprite runaction: [cctogglevisibility Action];} the 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: indicates that the function moves 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 move-ccmoveby jump to-ccjumpto to set the end position and the Skip height and number of times. Jump-ccjumpby: set the end position and the height and number of hops. The beiser-ccbezierby curve supports three times: 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 to set the zoom in factor, which is a floating point type. Zoom in-ccscaleby rotate to-ccrotateto rotate-ccrotateby flicker-ccblink sets the number of flashes the tone changes to-cctintto tone change- intby dimmed to-ccfadeto changed from light-ccfadein to light-ccfadeout the combination action combines the above basic actions in a certain order, form a coherent set of combined actions. Combined Actions include the following types: sequence-ccsequence is used 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];} synchronous-spawn is used to concurrently execute several actions, however, 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];} example code of an action with a limited number of repeate repetitions:-(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,-20 0) Height: 30 jumps: 5]; Id AC3 = [ccjumpby actionwithduration: 2 position: CCP (S. width/2, 0) Height: 20 jumps: 3]; Id seq = [ccsequence actions: AC1, ac2, AC3, nil]; // repeat the preceding sequence three times. [Sprite runaction: [ccrepeat actionwithaction: seq times: 3];} reactionary action-reverse reactionary action is to reverse (reverse) execute an action and support the reactionary 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 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];} infinite repetition-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];} the basic and composite actions of speed changes 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 from slow to fast. Easeout from fast to slow easeinout from slow to fast and then from fast to slow. Easesinein from slow to fast easesineout from fast to slow easesineinout 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. Expansion action we have mastered the execution of a variety of actions, but also according to the speed of the operation to modify the execution time, Cocos2D-iPhone also provides for the existing action expansion, to achieve a variety of flexible results. Delayed action-delay adds 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 functions call a function in the middle of the action sequence or end the call of a function to execute any tasks that need to be executed: Action, State 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 implement arbitrary expansion and connection of the action and action sequence)-(void) callback1 {[sprite runaction: [cctintby actionwithduration: 0.5 Red: 255 Green: 0 Blue: 255];} with object parameters When a user-defined function is called, 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];} Custom calls with objects and data Parameters When a function is used, 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];} corresponding user-defined function, we use the passed object 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.