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

  • 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...
主站蜘蛛池模板: 探索| 邵东县| 龙州县| 德州市| 大埔区| 徐州市| 蕉岭县| 洱源县| 呼伦贝尔市| 高安市| 田林县| 黄石市| 辰溪县| 宣汉县| 册亨县| 石嘴山市| 普格县| 申扎县| 武定县| 同仁县| 青海省| 闻喜县| 元朗区| 红原县| 甘孜| 渭源县| 临高县| 崇明县| 剑川县| 池州市| 景宁| 上高县| 延庆县| 修水县| 柯坪县| 锡林浩特市| 广灵县| 潜江市| 深泽县| 永靖县| 江山市|