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

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...
主站蜘蛛池模板: 旺苍县| 湖南省| 开阳县| 洪雅县| 宁夏| 沐川县| 灵石县| 抚州市| 麦盖提县| 吉首市| 隆子县| 南皮县| 临高县| 九台市| 万年县| 沐川县| 遵义县| 深圳市| 汕尾市| 醴陵市| 延吉市| 阳东县| 仙游县| 合肥市| 孝昌县| 大新县| 德庆县| 乌拉特前旗| 东源县| 建德市| 浮山县| 连山| 密山市| 阜康市| 余庆县| 梁平县| 富裕县| 永寿县| 阳春市| 综艺| 福泉市|