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

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...
主站蜘蛛池模板: 中江县| 万州区| 年辖:市辖区| 瓦房店市| 栾川县| 闽清县| 秦皇岛市| 澳门| 怀柔区| 福安市| 湖北省| 怀集县| 龙里县| 铜鼓县| 镇坪县| 扶绥县| 仁布县| 卓资县| 土默特右旗| 公安县| 汉沽区| 衡南县| 肃宁县| 锦州市| 遂平县| 遵义县| 齐齐哈尔市| 大田县| 阿尔山市| 伽师县| 吕梁市| 新干县| 凭祥市| 宣恩县| 安岳县| 保亭| 内丘县| 五寨县| 东宁县| 牙克石市| 沈阳市|