【Cocos2d-x 3.x】 動作類Action學習
遊戲設計中,動作是不可缺少的,Cocos2d-x中所有的動作都繼承自Action類,而Action類繼承自Ref和Clonable類,整個動作類繼承體系如圖:
FiniteTimeAction是所有瞬時動作和延時動作的父類,Follow跟隨一個節點的動作,Speed改變一個動作的時間。,其中FiniteTimeAction的兩個子類以及這兩個子類的子類是重點。
遊戲設計中,動作是不可缺少的,Cocos2d-x中所有的動作都繼承自Action類,而Action類繼承自Ref和Clonable類,整個動作類繼承體系如圖:
FiniteTimeAction是所有瞬時動作和延時動作的父類,Follow跟隨一個節點的動作,Speed改變一個動作的時間。,其中FiniteTimeAction的兩個子類以及這兩個子類的子類是重點。
瞬時性動作類
<ActionInstant.h>中的類是瞬時動作類,它的子類有:
//... 顯示一個節點 setVisible(true)
class Show : public ActionInstant {
};
//... 隱藏一個節點 setVisible(false)
class Hide : public ActionInstant {
};
//... 切換節點的可視屬性 setVisible(!_target->isVisible())
class ToggleVisibility : public ActionInstant {
};
//... 移除自己
class RemoveSelf : public ActionInstant {
};
// 水平翻轉精靈
class FlipX : public ActionInstant {
};
// 垂直翻轉精靈
class FlipY : public ActionInstant {
};
// 將節點放置到某個位置
class Place : public ActionInstant {
};
// 設置動作的回調函數為 std::function<void()>
class CallFunc : public ActionInstant {
public :
static CallFunc * create(const std::function<void()>& func);
};
// 設置動作的回調函數為 std::function<void(Node*)>
class CallFuncN : public CallFunc {
public :
static CallFuncN * create(const std::function<void(Node*)>& func);
};
<ActionInstant.h>中的類是瞬時動作類,它的子類有:
//... 顯示一個節點 setVisible(true)
class Show : public ActionInstant {
};
//... 隱藏一個節點 setVisible(false)
class Hide : public ActionInstant {
};
//... 切換節點的可視屬性 setVisible(!_target->isVisible())
class ToggleVisibility : public ActionInstant {
};
//... 移除自己
class RemoveSelf : public ActionInstant {
};
// 水平翻轉精靈
class FlipX : public ActionInstant {
};
// 垂直翻轉精靈
class FlipY : public ActionInstant {
};
// 將節點放置到某個位置
class Place : public ActionInstant {
};
// 設置動作的回調函數為 std::function<void()>
class CallFunc : public ActionInstant {
public :
static CallFunc * create(const std::function<void()>& func);
};
// 設置動作的回調函數為 std::function<void(Node*)>
class CallFuncN : public CallFunc {
public :
static CallFuncN * create(const std::function<void(Node*)>& func);
};
延時性動作類
<ActionInterval.h>中的類是瞬時動作類,它的子類有:
// 創建序列動畫
class Sequence : public ActionInterval {
public :
// 這種方式創建序列動畫最後需要加nullptr
// 比如: Sequence::create(action1, action2, nullptr);
static Sequence* create(FiniteTimeAction *action1, ...);
// 根據一個動作vector來創建
static Sequence* create(const Vector<FiniteTimeAction*>& arrayOfActions);
// 創建兩個動作
static Sequence* createWithTwoActions(FiniteTimeAction *actionOne, FiniteTimeAction *actionTwo);
// 根據變長動作數組創建序列動作
static Sequence* createWithVariableList(FiniteTimeAction *action1, va_list args);
};
// 重複一個動作(一次)
class Repeat : public ActionInterval {
public :
// 創建一個FiniteTimeAction動作
static Repeat* create(FiniteTimeAction *action, unsigned int times);
};
// 創建不斷重複的動作
class RepeatForever : public ActionInterval {
public :
// 由一個延時動作ActionInterval而創建
static RepeatForever* create(ActionInterval *action);
};
// 創建同時執行的動作
class Spawn : public ActionInterval {
public :
// 同序列式動作, 最後需要添加nullptr
static Spawn* create(FiniteTimeAction *action1, ...);
static Spawn* createWithVariableList(FiniteTimeAction *action1, va_list args);
static Spawn* create(const Vector<FiniteTimeAction*>& arrayOfActions);
static Spawn* createWithTwoActions(FiniteTimeAction *action1, FiniteTimeAction *action2);
};
// 旋轉動作 旋轉到某個角度
class RotateTo : public ActionInterval {
};
// 旋轉動作 旋轉一定角度
class CC_DLL RotateBy : public ActionInterval {
};
// 移動一定距離
class MoveBy : public ActionInterval {
};
// 移動到某個點
class MoveTo : public MoveBy {
};
/*---------- 這個動作By版本繼承自To版本 ----------*/
// 使某個傾斜到某個角度
class SkewTo : public ActionInterval {
};
// 傾斜一定角度
class SkewBy : public SkewTo {
};
// 跳躍一定距離
class JumpBy : public ActionInterval {
};
// 跳躍到某個點
class JumpTo : public JumpBy {
};
// 貝塞爾曲線
class BezierBy : public ActionInterval {
};
//
class BezierTo : public BezierBy {
};
// 縮放
class ScaleTo : public ActionInterval {
};
class ScaleBy : public ScaleTo {
};
// 閃爍
class Blink : public ActionInterval {
};
// 設置透明度
class FadeTo : public ActionInterval
// 淡入
class FadeIn : public FadeTo {
};
// 淡出
class FadeOut : public FadeTo {
};
// 延時動作
class DelayTime : public ActionInterval {
};
// 動畫
class Animate : public ActionInterval {
};
在所有延時動作裡:
- SkewBy繼承自SkewTo, ScaleBy繼承自ScaleTo
- RotateBy和RotateTo分別繼承自ActionInterval
TinkTo和TinkBy分別繼承自ActionInterval
- BesizerTo繼承自BezierBy
MoveTo繼承自MoveBy
JumpTo繼承自JumpBy
<ActionInterval.h>中的類是瞬時動作類,它的子類有:
// 創建序列動畫
class Sequence : public ActionInterval {
public :
// 這種方式創建序列動畫最後需要加nullptr
// 比如: Sequence::create(action1, action2, nullptr);
static Sequence* create(FiniteTimeAction *action1, ...);
// 根據一個動作vector來創建
static Sequence* create(const Vector<FiniteTimeAction*>& arrayOfActions);
// 創建兩個動作
static Sequence* createWithTwoActions(FiniteTimeAction *actionOne, FiniteTimeAction *actionTwo);
// 根據變長動作數組創建序列動作
static Sequence* createWithVariableList(FiniteTimeAction *action1, va_list args);
};
// 重複一個動作(一次)
class Repeat : public ActionInterval {
public :
// 創建一個FiniteTimeAction動作
static Repeat* create(FiniteTimeAction *action, unsigned int times);
};
// 創建不斷重複的動作
class RepeatForever : public ActionInterval {
public :
// 由一個延時動作ActionInterval而創建
static RepeatForever* create(ActionInterval *action);
};
// 創建同時執行的動作
class Spawn : public ActionInterval {
public :
// 同序列式動作, 最後需要添加nullptr
static Spawn* create(FiniteTimeAction *action1, ...);
static Spawn* createWithVariableList(FiniteTimeAction *action1, va_list args);
static Spawn* create(const Vector<FiniteTimeAction*>& arrayOfActions);
static Spawn* createWithTwoActions(FiniteTimeAction *action1, FiniteTimeAction *action2);
};
// 旋轉動作 旋轉到某個角度
class RotateTo : public ActionInterval {
};
// 旋轉動作 旋轉一定角度
class CC_DLL RotateBy : public ActionInterval {
};
// 移動一定距離
class MoveBy : public ActionInterval {
};
// 移動到某個點
class MoveTo : public MoveBy {
};
/*---------- 這個動作By版本繼承自To版本 ----------*/
// 使某個傾斜到某個角度
class SkewTo : public ActionInterval {
};
// 傾斜一定角度
class SkewBy : public SkewTo {
};
// 跳躍一定距離
class JumpBy : public ActionInterval {
};
// 跳躍到某個點
class JumpTo : public JumpBy {
};
// 貝塞爾曲線
class BezierBy : public ActionInterval {
};
//
class BezierTo : public BezierBy {
};
// 縮放
class ScaleTo : public ActionInterval {
};
class ScaleBy : public ScaleTo {
};
// 閃爍
class Blink : public ActionInterval {
};
// 設置透明度
class FadeTo : public ActionInterval
// 淡入
class FadeIn : public FadeTo {
};
// 淡出
class FadeOut : public FadeTo {
};
// 延時動作
class DelayTime : public ActionInterval {
};
// 動畫
class Animate : public ActionInterval {
};
在所有延時動作裡:
- SkewBy繼承自SkewTo, ScaleBy繼承自ScaleTo
- RotateBy和RotateTo分別繼承自ActionInterval
TinkTo和TinkBy分別繼承自ActionInterval - BesizerTo繼承自BezierBy
MoveTo繼承自MoveBy
JumpTo繼承自JumpBy
動作管理
ACTIONS
version: Cocos2d-x v3.x
update: 於 超過 1 年 前更新
Action
objects are just like they sound, make a Node
perform a change to its properties. Action
objects allow the transform of Node
properties in time. Any object with a base class of Node
can have Action
objects performed on it. As an example, you can move a Sprite
from one position to another and do it over a time span.
Example of
MoveTo
and MoveBy
action:
By and To, what is the difference?
You will notice that each
Action
has a By
and To
version. Why? They are different in what they accomplish. A By
is relative to the current state of the Node
. A To
action is absolute, meaning it doesn't take into account the current state of the Node
. Let's take a look at a specific example:Basic Actions and how to run them
Basic actions are usually a singular action, thus accomplishing a single objective. Let's take a look at a few examples:
Move
Move the
Node
position over a set period of time.Rotate
Rotates a
Node
clockwise over 2 seconds.Scale
Scales a
Node
by 10 over 2 seconds.Fade In/Out
Fades In a
Node
.
It modifies the opacity from 0 to 255. The "reverse" of this action is
FadeOut
Tint
Tints a Node that implements the NodeRGB protocol from current tint to a custom one.
Animate
With
Animate
it is possible to do simple flipbook animation with your Sprite
objects. This is simply replacing the display frame at set intervals for the duration of the animation. Let's consider this example:
It's hard to show an animation in text, so please run the example "Programmer Guide Sample" code to see this in action!
Easing
Easing is animating with a specified acceleration to make the animations smooth. A few things to keep in mind is that regardless of speed, ease actions always
start and finish at the same time. Ease actions are a good way to fake physics in your game! Perhaps you want a few simulated physics effects but dont want the
overhead and complexity of adding it all for a few very basic actions. Another good example is to animate menus and buttons.
start and finish at the same time. Ease actions are a good way to fake physics in your game! Perhaps you want a few simulated physics effects but dont want the
overhead and complexity of adding it all for a few very basic actions. Another good example is to animate menus and buttons.
Here are common easing functions displayed over a graph:
Cocos2d-x supports most of the easing function in the above graph. They are also simple to implement. Lets look at a specific use case. Lets drop a
from the top of the screen and make it bounce.
Sprite
objectfrom the top of the screen and make it bounce.
Run the example "Programmer Guide Sample" code to see this in action!
Sequences and how to run them
Sequences are a series of
Action
objects to be executed sequentially. This can be any number ofAction
objects, Functions and even another Sequence
. Functions? Yes! Cocos2d-x has aCallFunc
object that allows you to create a function() and pass it in to be run in your Sequence
. This allows you to add your own functionality to your Sequence
objects besides just the stockAction
objects that Cocos2d-x provides. This is what a Sequence
looks like when executing:An example sequence
So what is this
Sequence
action do?
It will executes the following actions sequentially:
Jump
-> callbackJump
-> Rotate
-> callbackRotate
Run the example "Programmer Guide Sample" code to see this in action!
Spawn
Spawn is very similar to
Sequence
, except that all actions will run at the same time. You can have any number of Action
objects and even other Spawn
objects!Spawn
produces the same result as running multiple consecutive runAction()
statements. However, the benefit of spawn is that you can put it in a Sequence
to help achieve specific effects that you cannot otherwise. Combining
Spawn
and Sequence
is a very powerful feature
Example, given:
Using a Spawn:
and consecutive runAction() statements:
Both would produce the same result. However, one can use
Spawn
in a Sequence
. This flowchart shows the how this might look:
Run the example "Programmer Guide Sample" code to see this in action!
Reverse
Reverse is exactly like it sounds. If you run a series of actions, you can call
simply running in reverse. It is actually manipulating the properties of the original
reverse()
to run it in the opposite order. Backwards. However it is not justsimply running in reverse. It is actually manipulating the properties of the original
Sequence
orSpawn
in reverse too.
Using the
Spawn
example above reversing is simple.
Most
Action
and Sequence
objects are reversible!
It's easy to use, but let's make sure we see what is happening. Given:
What is really happening? If we lay out the steps as a list it might be helpful:
mySprite
is createdmySprite
position is set to (50, 56)sequence
starts to runsequence
movesmySprite
by 500, over 2 seconds,mySprite
new position (550, 56)sequence
delays for 2 secondssequence
scalesmySprite
by 2x over 2 secondssequence
delays for 6 more seconds (notice we run another sequence to accomplish this)- we run a
reverse
on the sequence so we re-run each action backwards sequence
is delayed for 6 secondssequence
scalesmySprite
by -2x over 2 secondssequence
delays for 2 secondssequence
movesmySprite
by -500, over 2 seconds,mySprite
new position (50, 56)
You can see that a
reverse()
is simply for you to use, but not so simple in it's internal logic. Cocos2d-x does all the heavy lifting!
沒有留言:
張貼留言