cocos2d-x 4.0 學習之路(十一)連續的動作(Sequence和Spawn)

有了Action(MoveTo,ScaleBy等)的基礎動作,cocos爲我們提供了一些方法,把這些動作連起來。

Sequence

按順序或者說是按序列的執行動作。
比如下面的這段代碼:

    auto jump = JumpBy::create(1.5, Vec2(0, 0), 100, 3);
    auto rotate = RotateTo::create(2.0f, 90);
    sprite1->runAction(Sequence::create(jump, rotate, nullptr));

它描述了,先jump 後 rotate這樣的動作順序。callback是啥,sequence不光可以加載定義好的各種動作,還可以加載自己定義的動作,這就是CallFunc定義的東西。這裏面只是輸出了log。(輸出的log內容只能在調試下可以看到,也就是隻按F5)
在這裏插入圖片描述
各個動作之間如果想加入一些延遲怎麼弄?可以用DelayTime。下面這樣就是在跳躍和旋轉停頓5秒。

DelayTime* delay = DelayTime::create(5);
sprite1->runAction(Sequence::create(jump, callbackJump, delay, rotate, callbackRotate, nullptr));

Spawn

Sequence是順序執行的,而Spawn是並列執行的。也就是放入Spawn裏面的動作是一起被執行的。

    auto moveBy = MoveBy::create(2, Vec2(400, 100));
    auto fadeTo = FadeTo::create(2.0f, 120.0f);
    auto mySpawn = Spawn::createWithTwoActions(moveBy, fadeTo);
    sprite1->runAction(mySpawn);

在這裏插入圖片描述
那麼我們有了這兩種工具,還可以把Spawn嵌入到Sequence裏,實現更多的效果:

    auto moveBy = MoveBy::create(1, Vec2(400, 100));
    auto fadeTo = FadeTo::create(2.0f, 120.0f);
    auto scaleBy = ScaleBy::create(2.0f, 3.0f);
    auto mySpawn = Spawn::createWithTwoActions(scaleBy, fadeTo);
    sprite1->runAction(Sequence::create(moveBy, mySpawn, nullptr));

在這裏插入圖片描述

RepeatForever

RepeatForever會讓動作永遠的執行下去,用法也很簡單:

    auto moveBy = MoveBy::create(1.0f, Vec2(500, 0));
    auto scaleBy = ScaleBy::create(1.0f, 2.0f);
    auto mySpawn = Spawn::createWithTwoActions(moveBy, scaleBy);
    auto sequence = Sequence::create(mySpawn, mySpawn->reverse(), nullptr);
    sprite1->runAction(RepeatForever::create(sequence));

在這裏插入圖片描述
那麼,想要控制循環動作幾次呢?只需要用Repeat就行了:

sprite1->runAction(Repeat::create(sequence, 3));	//重複執行3次
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章