ผลต่างระหว่างรุ่นของ "01219245/cocos2d/Actions"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 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.
 +
 +
Add method <tt>fall</tt> to class <tt>Player</tt>:
 +
 +
<syntaxhighlight lang="javascript">
 +
    fall: function() {
 +
var pos = this.getPosition();
 +
var fallAction = cc.MoveTo.create( 0.5, new cc.Point( pos.x, 0 ) );
 +
this.runAction( fallAction );
 +
    }
 +
</syntaxhighlight>
  
 
=== Flapping dot: animations and combining actions ===
 
=== Flapping dot: animations and combining actions ===
  
 
== Spritesheets ==
 
== Spritesheets ==

รุ่นแก้ไขเมื่อ 02:10, 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 action to tell the object to move to a specific location.

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 );
    }

Flapping dot: animations and combining actions

Spritesheets