ผลต่างระหว่างรุ่นของ "01219245/cocos2d/Actions"
Jittat (คุย | มีส่วนร่วม) |
Jittat (คุย | มีส่วนร่วม) |
||
แถว 11: | แถว 11: | ||
=== Falling dot === | === Falling dot === | ||
− | We shall let the player falls. In this case, we use <tt>cc.MoveTo</tt> action to tell the object to move to a specific location. After we create the action, we can tell the object to perform an action simply by calling method <tt>runAction</tt>. | + | We shall let the player falls. In this case, we use <tt>cc.MoveTo</tt> ([http://www.cocos2d-x.org/reference/html5-js/V2.2.2/symbols/cc.MoveTo.html ref]) action to tell the object to move to a specific location. After we create the action, we can tell the object to perform an action simply by calling method <tt>runAction</tt>. |
Add method <tt>fall</tt> to class <tt>Player</tt>: | Add method <tt>fall</tt> to class <tt>Player</tt>: |
รุ่นแก้ไขเมื่อ 02:12, 16 กุมภาพันธ์ 2557
- This is part of 01219245.
In this tutorial, we shall not create a new game, but we will learn a few useful techniques for making our game more engaging.
Actions
Using a combination of states and update method is very useful when we want to (micro) manage our objects. However, sometimes, we just want to tell object to do something like jumping or falling and do not want to keep track of these action.
For example, when our flappy dot dies, we want it to fall down to the ground. If we use states, we might want to add code to move the sprite downwards slighly in update method and to keep track that the dot reaches the ground. It will be easier if you can just tell the dot to fall down, and then stop worrying about it as the object shall move accordingly by itself (or by some magics). In Cocos2d-html5, there is a mechanism for performing that kind of work, called actions (See reference here, type action in to the search box). (Read an overview for C++ version in Cocos2d-x here.)
We shall learn how to use actions by examples.
Falling dot
We shall let the player falls. In this case, we use cc.MoveTo (ref) action to tell the object to move to a specific location. After we create the action, we can tell the object to perform an action simply by calling method runAction.
Add method fall to class Player:
fall: function() {
var pos = this.getPosition();
var fallAction = cc.MoveTo.create( 0.5, new cc.Point( pos.x, 0 ) );
this.runAction( fallAction );
}