官术网_书友最值得收藏!

  • 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...

Sequencing actions

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.

Spawning actions

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.

Repeating actions

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);

Reversing actions

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

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.

How it works...

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

How it works...

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);
There's more...
主站蜘蛛池模板: 城步| 揭阳市| 托克托县| 无为县| 青田县| 西青区| 和硕县| 许昌市| 广宁县| 阿合奇县| 寿阳县| 玉门市| 平南县| 临清市| 香港| 通化县| 山阳县| 建阳市| 阳曲县| 那坡县| 通城县| 涿鹿县| 乳山市| 彩票| 十堰市| 拉孜县| 河西区| 平江县| 忻州市| 宝坻区| 西安市| 定结县| 惠水县| 卓尼县| 伊宁县| 洞头县| 柘荣县| 新干县| 嵊泗县| 玉环县| 乌兰浩特市|