- Cocos2d-x Cookbook
- Akihiro Matsuura
- 775字
- 2021-07-09 20:58:49
Controlling actions
In the previous recipe, you learned some of the basic actions. However, you may want to use more complex actions; for example, rotating a character while moving, or moving a character after jumping. In this recipe, you will learn how to control actions.
How to do it...
Sequence
is a series of actions to be executed sequentially. This can be any number of actions.
auto move = MoveBy::create(2.0f, Vec2(100, 0)); auto rotate = RotateBy::create(2.0f, 360.0f); auto action = Sequence::create(move, rotate, nullptr); sprite->runAction(action);
The preceding command will execute the following actions sequentially:
- Move a sprite 100px to the right over two seconds
- Rotate a sprite clockwise by 360 degree over two seconds
It takes a total of four seconds to execute these commands.
Spawn
is very similar to Sequence
, except that all actions will run at the same time. You can specify any number of actions at the same time.
auto move = MoveBy::create(2.0f, Vec2(100, 0)); auto rotate = RotateBy::create(2.0f, 360.0f); auto action = Spawn::create(move, rotate, nullptr); sprite->runAction(action);
It will execute the following actions at the same time:
- Moved a sprite 100px to the right over two seconds
- Rotated a sprite clockwise by 360 degree over two seconds
It takes a total of two seconds to execute them.
Repeat
object is to repeat an action the number of specified times.
auto rotate = RotateBy::create(2.0f, 360.0f); auto action = Repeat::create(rotate, 5); sprite->runAction(action);
The preceding command will execute a rotate
action five times.
If you want to repeat forever, you can use the RepeatForever
action.
auto rotate = RotateBy::create(2.0f, 360.0f); auto action = RepeatForever::create(rotate); sprite->runAction(action);
If you generate an action
instance, you can call a reverse
method to run it in the reverse
action.
auto move = MoveBy::create(2.0f, Vec2(100, 0)); auto action = Sequence::create(move, move->reverse(), nullptr); sprite->runAction(action);
The preceding code will execute the following actions sequentially:
- Move a sprite 100px to the right over two seconds.
- Move a sprite 100px to the left over two seconds.
In addition, if you generate a sequence action, you can call a reverse
method to run it in the opposite order.
auto move = MoveBy::create(2.0f, Vec2(100, 0)); auto rotate = RotateBy::create(2.0f, 360.0f); auto sequence = Sequence::create(move, rotate, nullptr); auto action = Sequence::create(sequence, sequence->reverse(), nullptr); sprite->runAction(action);
The preceding code will execute the following actions sequentially:
- Move a sprite 100px to the right over two seconds.
- Rotate a sprite clockwise by 360 degree over two seconds
- Rotate a sprite counterclockwise by 360 degree over two seconds
- Move a sprite 100px to the left over two seconds.
DelayTime
is a delayed action within the specified number of seconds.
auto move = MoveBy::create(2.0f, Vec2(100, 0)); auto delay = DelayTime::create(2.0f); auto rotate = RotateBy::create(2.0f, 360.0f); auto action = Sequence::create(move, delay, rotate, nullptr); sprite->runAction(action);
The preceding command will execute the following actions sequentially:
- Move a sprite 100px to the right over two seconds
- Delay the next action by two seconds
- Rotate a sprite clockwise by 360 degree over two seconds
It takes a total of six seconds to execute it.
How it works...
Sequence
action runs actions sequentially. You can generate a Sequence
instance with actions sequentially. Also, you need to specify nullptr
last. If you did not specify nullptr
, your game will crash.

Spawn
action runs actions at the same time. You can generate a Spawn
instance with actions and nullptr
like Sequence
action.

Repeat
and RepeatForever
actions can run, repeating the same action. Repeat
action has two parameters, the repeating action and the number of repeating actions. RepeatForever
action has one parameter, the repeating action, which is why it will run forever.
Most actions, including Sequence
, Spawn
and Repeat,
have the reverse
method. But like the MoveTo
method that has the suffix To
, it does not have the reverse
method; that's why it cannot run the reverse action. Reverse
method generates its reverse action. The following code uses the MoveBy::reverse
method.
MoveBy* MoveBy::reverse() const { return MoveBy::create(_duration, -_positionDelta); }
DelayTime
action can delay an action after this. The benefit of the DelayTime
action is that you can put it in the Sequence
action. Combining DelayTime
and Sequence
is a very powerful feature.
There's more...
Spawn
produces the same results as running multiple consecutive runAction
statements.
auto move = MoveBy::create(2.0f, Vec2(100, 0)); auto rotate = RotateBy::create(2.0f, 360.0f); sprite->runAction(move); sprite->runAction(rotate);
However, the benefit of Spawn
is that you can put it in the Sequence
action. Combining Spawn
and Sequence
is a very powerful feature.
auto move = MoveBy::create(2.0f, Vec2(100, 0)); auto rotate = RotateBy::create(2.0f, 360.0f); auto fadeout = FadeOut::create(2.0f); auto spawn = Spawn::create(rotate, fadeout, nullptr); auto fadein = FadeIn::create(2.0f); auto action = Sequence::create(move, spawn, fadein, nullptr); sprite->runAction(action);

- Visual FoxPro程序設(shè)計(jì)教程(第3版)
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)實(shí)踐教程
- 區(qū)塊鏈:以太坊DApp開(kāi)發(fā)實(shí)戰(zhàn)
- JSP開(kāi)發(fā)案例教程
- 小學(xué)生C++創(chuàng)意編程(視頻教學(xué)版)
- C語(yǔ)言程序設(shè)計(jì)簡(jiǎn)明教程:Qt實(shí)戰(zhàn)
- 精通MySQL 8(視頻教學(xué)版)
- JavaScript+jQuery網(wǎng)頁(yè)特效設(shè)計(jì)任務(wù)驅(qū)動(dòng)教程
- UI動(dòng)效設(shè)計(jì)從入門(mén)到精通
- 3ds Max 2018從入門(mén)到精通
- VMware vSphere Design Essentials
- PHP程序設(shè)計(jì)高級(jí)教程
- 開(kāi)源網(wǎng)絡(luò)地圖可視化:基于Leaflet的在線地圖開(kāi)發(fā)
- Scratch少兒編程高手的7個(gè)好習(xí)慣
- 程序員的算法趣題2