[Nagging]
In a movie, a role's movement is an action. In the game, animation is the role action. Such as walking, jumping, releasing magic, flying birds, and rolling wheels. Action is an indispensable part of the game, making the game more attractive and energetic.
The cocos2dx engine provides a wide range of action systems. This section describes the most basic actions in the cocos2dx action system.
This section has many contents and needs to be digested slowly ......
[Thank you]
Http://gl.paea.cn/contents/fdb2fb923944b2e6.html
"Cocos2D-X game development technical explanation" Liu jianzhuo (don't misunderstand, I am not to sell the book 650) This. width = 650; "src =" http://img.baidu.com/hi/face/ I _f19.gif "alt =" I _f19.gif "/>)
[Ccaction]
Ccaction is the base class of all actions.
The inheritance relationship is as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/B8/wKiom1P-7iiClJuSAACQj8pwumA316.jpg "Title =" ccaction.jpg "alt =" wKiom1P-7iiClJuSAACQj8pwumA316.jpg "/>
We mainly focus on the ccaction subclass and its subclass.
(1)Ccfinitetimeaction: Time-related period class.
It can be divided into two sub-classes:
()Ccactioninstant: Instant action-related category.
(B)Ccactioninterval: Sequence class related to continuous actions.
(2)Ccfollow: Follows the category class.
(3)Ccspeed: Speed class.
Next, we will detail the three categories. Its organizational structure is as follows:
(0) Action Management
(1) instantaneous action ccactioninstant
(2) continuous action ccactioninterval
(3) cccallfunc
(4) combined action
(5) Variable Speed cceaseaction
(6) Speed ccspeed
(7) delay action ccdelaytime
(8) Follow action ccfollow
0. Action Management
Used to manage the action execution of an object. Such as running, pause, and stop.
Common Operations are as follows:
// SP-> runaction ("Action object"); // execute SP-> pauseschedulerandactions (); // suspend SP-> resumeschedulerandactions (); // continue SP-> stopaction ("Action object"); // stop an action of an object SP-> stopactionbytag ("tag value "); // stop the tag action SP-> stopallactions (); // stop all actions of the object //
1. instantaneous action ccactioninstant
Ccactioninstant inherits from ccfinitetimeactionIs an instant execution class. That is, the interval between the executed actions is zero.
Example:
// Instantly flip Y Action ccsprite * sp = ccsprite: Create ("icon.png"); ccflipy * flipy = ccflipy: Create (true); SP-> runaction (flipy ); //
Common actions are as follows:
/// Flip ccflipx: Create ('whether to Flip left or right bool '); ccflipy: Create ('whether to flip up or down bool'); // set the coordinate ccplace :: create ('location ccpoint'); // display, hide // that is: setvisible (), to set visible/invisible ccshow: Create (); cchide :: create (); // visible switch // make visible and invisible. Cctogglevisibility: Create ();//
2. Continuous action ccactioninterval
Ccactioninstant inherits from ccfinitetimeactionIs a continuous execution class. That is to say, during a period of time, an action is completed.
Example:
// Move to (1.0) ccsprite * sp = ccsprite: Create ("icon.png") within 200,200 seconds; ccmoveto * moveTo = ccmoveto: Create (1.0f, CCP (200,200 )); SP-> runaction (moveTo );//
Common actions are as follows:
/// *** About to and by: * to: absolute action. * By: relative action. ** For example, the coordinates of the current object are (20, 20 ). * After ccmoveto (50, 50), it is moved to the position (50, 50. * After ccmoveby (50, 50), it is moving relative to the current coordinate, and the final coordinate is (70, 70 ). * // *** Movement related ** // a few seconds later, move to the coordinate point ccmoveto: Create ("time", "coordinate"); ccmoveby :: create ("time", "coordinate"); // a few seconds later, after several jumps to the specified position ccjumpto: Create ("time", "target location ", "Height", "Number of times required to reach the target"); ccjumpby: Create ("time", "target location", "height", "Number of times required to reach the target "); // The ccbezierto: Create ("time", "ccbezierconfig constructor"); ccbezierby: Create ("time ", "ccbezierconfig constructor"); // the number of seconds that follow the curve (the softest of fitting degree 0) cccardinalsplineto: Create ("time", "Control Point Coordinate array ", "Fit degree"); cccardinalsplineby: Create ("time", "Control Point Coordinate array", "Fit degree"); // complete a spline interpolation track (straight line) in seconds) cccatmullromto: Create ("time", "Control Point Coordinate array"); cccatmullromby: Create ("time", "Control Point Coordinate array "); // sphere motion within several seconds ccorbitcamera: Create ("time", "Starting radius", "radius difference", "Starting Z angle", "Rotating Z angle ", "Start x angle", "rotate x angle");/***** contraction related * // a few seconds later it is scaled down to the specified size (1: original size; greater than 1: enlarged; smaller than 1: Zoom out) ccscaleto: Create ("time", "zoom in ratio"); ccscaleby: Create ("time", "zoom in ratio "); /*** Rotation Degree ** // The number of seconds after which the image is rotated. Unit: Angle ccrotateto: Create ("time", "angle"); ccrotateby :: create ("time", "angle");/*** tilt related * // specifies the angle after several seconds. Unit: Angle ccskewto: Create ("time ", "X axis angle", "Y axis angle"); ccskewby: Create ("time", "X axis angle", "Y axis angle "); /*** color-related ** // after several seconds, it becomes the specified RGB color. The color value is [0,255] cctintto: Create ("time", "Red", "green ", "Blue"); cctintby: Create ("time", "Red", "green", "blue"); // sets transparency [0,255] (255 is not transparent) ccfadein: Create ("time"); // fade in, transparency from 0 to 255 ccfadeout: Create ("time"); // fade out, transparency from 255 to 0 ccfadeto:: Create (1.0f, 80); // The transparency is converted to the specified value. // The ccblink will flash several times in a few seconds: Create ("time", "times ");//
3. cccallfunc
Cccallfunc is also a subclass of the instantaneous action ccactioninstant..It mainly has three types of function callback classes. The difference between the three function Callbacks is that the number of parameters included is 0, 1, and 2.
// Cccallfunc: Create ('object this ', 'callback function'); // callback function: without the parameter cccallfuncn: Create ('object this ', 'callback function'); // callback function: transmits itself as a parameter (ccnode * node) cccallfuncnd: Create ('object this ', 'callback function ', 'void'); // callback function: with two parameters (ccnode * node, void * )//
Usage:
/// Define the function callback action cccallfunc * back1 = cccallfunc: Create (this, callfunc_selector (mylayer: funback1); cccallfuncn * back2 = cccallfuncn: Create (this, callfuncn_selector (mylayer: funback2); cccallfuncnd * back3 = cccallfuncnd: Create (this, callfuncnd_selector (mylayer: funback3), (void *) 10 ); // callback function void mylayer: funback1 (){...} // cccallfunc callback function void mylayer: funback2 (ccnode * node ){...} // cccallfuncn callback function void mylayer: funback3 (ccnode * node, void * ){...} // cccallfuncnd callback function //
4. Combined Action
The combination action, as its name implies, is to combine a single action to form a more complex action.
For example, when moving, rotating, and performing function callback after bounce ......
The composite action class is also a subclass of ccactioninterval.There are two main types: sequential action and repeated action.
(1) sequential action:The order in which actions are executed.
// Ccspawn: Create ("Action object 1", "Action object 2 ",..., null); // The ccsequence: Create ("Action object 1", "Action object 2 ",..., null); // actions are executed in sequence //
(2) repeated actions:The action object can also be a sequential action.
// Ccrepeat: Create ("Action object", "times"); // ccrepeatforever: Create ("Action object"); // infinite repetition //
Usage:
// Flashes three times after each movement and repeats infinitely. Ccsprite * sp = ccsprite: Create ("icon.png"); ccmoveby * Move = ccmoveby: Create (1.0f, CCP (10, 10); ccblink * blink = ccblink :: create (1.0f, 3); ccsequence * seq = ccsequence: Create (move, blink, null); ccrepeatforever * repeatforever = ccrepeatforever: Create (SEQ ); SP-> runaction (repeatforever );//
5. Variable Speed cceaseaction
Cceaseaction is also a subclass of ccactioninterval.The characteristic of such actions is that the speed of the actions can change during execution. For example, ccmoveto can be accelerated or slowed down for moving, or accelerated and then slowed down.
This type of action exists because some actions in the game are not executed evenly, just like the freely falling ball, the falling speed will get faster and faster, rather than falling at a constant speed. Therefore, the cocos2dx engine encapsulates some frequently used classes with variable speed.
Such speed changes can be roughly divided into three types:
(1)In: Changes from slow to fast.
(2)Out: Slow down.
(3)Inout: Changes from slow to slow.
The speed changes are based on some physical formulas. Such as sine and exponent.
It is worth noting that:Cceaseaction changes the speed of a continuous action.The execution time remains unchanged..
Commonly used:
The changes in cceasein, cceaseout, and cceaseinout are a bit complicated. For details, refer to the following article:
Http://www.cnblogs.com/cocos2d-x/archive/2012/03/15/2398516.html
//// Linear variation. Cceasein: Create ("Action object", "adding rate"); // from slow to fast cceaseout: Create ("Action object", "adding rate "); // from fast to slow cceaseinout: Create ("Action object", "adding rate"); // Changes from slow to fast to slow // sine. Cceasesinein: Create ("Action object"); // from slow to fast cceasesineout: Create ("Action object"); // from fast to slow cceasesineinout :: create ("Action object"); // Changes from slow to fast to slow // index. The speed changes increase exponentially. Cceaseexponentialin: Create ("Action object"); // slow start cceaseexponentialout: Create ("Action object"); // slow end cceaseexponentialinout :: create ("Action object"); // starts slowly and ends slowly. // rebound changes. Similar to the ball hitting the ground, it keeps falling and rebounding. Cceasebouncein: Create ("Action object"); // rebound from the starting point cceasebounceout: Create ("Action object"); // rebound from the ending point cceasebounceinout :: create ("Action object"); // the start point and end point all rebound // The Force return change. It is similar to a bow and a back-to-Force mark. Cceasebackin: Create ("Action object"); // the start point is used as the return point cceasebackout: Create ("Action object"); // The end point is used as the return point cceasebackinout :: create ("Action object"); // the start point and end point are regarded as the return point // scaling type change. Cceaseelasticin: Create ("Action object"); // The starting point has the elastic cceaseelasticout: Create ("Action object"); // The end point has the elastic cceaseelasticinout :: create ("Action object"); // the start and end points are elastic //
Usage:
//CCSprite* sp = CCSprite::create("Icon.png");CCMoveBy* moveBy = CCMoveBy::create(5.0f, ccp(300, 300));CCEaseExponentialIn* easeMove = CCEaseExponentialIn::create(moveBy);sp->runAction(easeMove);//
6. Speed ccspeed
The speed class ccspeed has nothing to do with cceaseaction. It is neither an instantaneous action nor a continuous action. From the inheritance relationship,It directly inherits from ccaction.
It is used to change the execution rate of action, which isChanged the time required to execute the action.. It is like watching a movie. When I think the story is too slow, I can play it at twice the speed.
The usage is as follows:
// Execute moveBy at a speed of five times. It takes only 1.0 seconds to complete the action. Ccsprite * sp = ccsprite: Create ("icon.png"); ccmoveby * moveBy = ccmoveby: Create (5.0f, CCP (300,300); ccspeed * speed = ccspeed :: create (moveBy, 5.0); SP-> runaction (speed );//
7. delayed action ccdelaytime
Ccdelaytime is the ccactioninterval class.It is used to add a waiting period between two actions in the sequence action ccsequence. For example, wait 3 seconds after the mobile action is completed, and then perform the blinking action.
The usage is as follows:
// After moving, wait 3 seconds and then execute the blinking action ccsprite * sp = ccsprite: Create ("icon.png"); ccmoveby * moveBy = ccmoveby: Create (2.0f, CCP (300,300); ccblink * blink = ccblink: Create (2.0f, 10); ccdelaytime * Delay = ccdelaytime: Create (3.0); ccsequence * seq = ccsequence :: create (moveBy, delay, blink, null); SP-> runaction (SEQ );//
8. Follow the action ccfollow
Ccfollow is also a subclass that directly inherits ccaction.It allows a ccnode object to move along with another ccnode object synchronously.
As shown in, layers and sprite images are moved simultaneously.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/C0/wKiom1P_UNHiW2hWAApW6Y3K9tg228.gif "Title =" 1.gif" alt = "wkiom1p_unhiw2hwaapw6y3k9tg228.gif"/>
Usage:
/// *** Ccfollow: Create ("target object to be followed", "Scope to be followed ccrect "); * This range is to restrict the moving area of the following object * // obtain the screen size. ccsize mysize = ccdirector: shareddirector ()-> getvisiblesize (); // bg.png size: 1000x320. Ccsprite * BG = ccsprite: Create ("bg.png"); BG-> setposition (ccpointzero); BG-> setanchorpoint (ccpointzero); this-> addchild (BG ); // create the sprite spccsprite * sp = ccsprite: Create ("icon.png"); SP-> setposition (CCP (0,160); this-> addchild (SP ); // SP executes the move action. It moves to (1000,160) ccmoveto * moveTo = ccmoveto in 5 seconds: Create (5.0f, CCP (1000,160); SP-> runaction (moveTo ); // layer this follows the SP movement and the following range is 1000 * 320ccfollow * Follow = ccfollow: Create (SP, ccrectmake (0, 0, 1000,320 )); this-> runaction (follow );//
[Code practice]
There are too many content, please implement it on your own. Refer to the official testcpp project.
Tip: You can use the combined action to combine different actions, which may have a variety of magical effects.
[Demo download]
None.
This article is from the "summer wind" blog, please be sure to keep this source http://shahdza.blog.51cto.com/2410787/1546343
Cocos2dx BASICS (21) -- basic action ccaction