Motion 组件

事件

Moved [Old position = { axis: 'x' | 'y', oldValue: Number }]
当实体因在x或y轴上的速度/加速度而移动时,会触发一个移动事件。如果实体在两个轴上都移动了两个轴,那么这个事件就会触发两次。
NewDirection [New direction = { x: -1 | 0 | 1, y: -1 | 0 | 1 }]
当实体由于x或y轴上的速度而改变方向时,会触发一个新的方向事件。如果方向与上一帧不同,则触发一次事件。
MotionChange [Motion property name and old value = { key: String, oldValue: Number }]
当一个动作属性发生改变时,会触发一个MotionChange事件。

允许通过应用线性速度和加速度来移动一个实体。所有的线性运动值都以像素/秒表示(例如,一个vx为1的实体会在x轴上移动1px)。

注意:有几个方法返回矢量二维对象,这些对象动态地反映实体的底层属性。如果您想要一个静态副本,请使用clone()方法。

属性

方法

Back to top

.ax

一种用于在x轴上访问/修改线性加速度的属性。加速度随时间改变速度。

例子

var ent = Crafty.e("2D, Motion");

var ax = ent.ax; // retrieve the linear acceleration in the x axis
ent.ax += 1; // increase the linear acceleration in the x axis
ent.ax = 0; // reset the linear acceleration in the x axis
Back to top

.ay

一种用于在y轴上进行线性加速的属性。加速度随时间改变速度。

例子

var ent = Crafty.e("2D, Motion");

var ay = ent.ay; // retrieve the linear acceleration in the y axis
ent.ay += 1; // increase the linear acceleration in the y axis
ent.ay = 0; // reset the linear acceleration in the y axis
Back to top

.dx

一个数字反映了在最后一帧中应用的x的变化(旧的和新x的区别)。

例子

var ent = Crafty.e("2D, Motion");

var dx = ent.dx; // the change of x in the last frame
Back to top

.dy

一个数字反映了在最后一帧中应用的y的变化(旧的和新的y的区别)。

例子

var ent = Crafty.e("2D, Motion");

var dy = ent.dy; // the change of y in the last frame
Back to top

.acceleration()

访问/修改线性(x,y)加速度的方法。加速度随时间的增加而增加,从而导致速度的增加。

public Vector2D .acceleration()
[Returns]

加速度矢量,属性x,y,反映物体方向的加速度。

返回当前加速度。您可以访问/修改属性,以便检索/更改加速度。

例子

var ent = Crafty.e("2D, Motion");

var acc = ent.acceleration(); //returns the acceleration object
acc.x;       // retrieve the acceleration in the x direction
acc.x = 0;   // set the acceleration in the x direction
acc.x += 4   // add to the acceleration in the x direction
Back to top

.motionDelta()

public Vector2D .motionDelta()
[Returns]

一个带有x和y的向量,它反映了x和y的变化。

返回在上一帧中应用的旧和新位置的区别。

例子

var ent = Crafty.e("2D, Motion");

var deltaY = ent.motionDelta().y; // the change of y in the last frame
Back to top

.resetMotion()

public this .resetMotion()
[Returns]

this

重置所有的线性运动(重置速度、加速度、移动速度)。

Back to top

.velocity()

访问/修改线性(x,y)速度的方法。速度是恒定的,除非加速度增加了速度。

public Vector2D .velocity()
[Returns]

速度矢量,是在物体的方向上反映速度的。

返回当前的速度。您可以访问/修改属性,以便检索/更改速度。

例子

var ent = Crafty.e("2D, Motion");

var vel = ent.velocity(); //returns the velocity vector
vel.x;       // retrieve the velocity in the x direction
vel.x = 0;   // set the velocity in the x direction
vel.x += 4   // add to the velocity in the x direction
Back to top

.vx

在x轴上访问/修改线性速度的一种属性。速度保持不变,除非加速度改变速度。

例子

var ent = Crafty.e("2D, Motion");

var vx = ent.vx; // retrieve the linear velocity in the x axis
ent.vx += 1; // increase the linear velocity in the x axis
ent.vx = 0; // reset the linear velocity in the x axis
Back to top

.vy

在y轴上访问/修改线性速度的一种属性。速度保持不变,除非加速度改变速度。

例子

var ent = Crafty.e("2D, Motion");

var vy = ent.vy; // retrieve the linear velocity in the y axis
ent.vy += 1; // increase the linear velocity in the y axis
ent.vy = 0; // reset the linear velocity in the y axis
Back to top

.ccdbr()

public Object .ccdbr([Object ccdbr])
ccdbr

用作输出的对象

[Returns]

一个带有_x, _y, _w, 和 _h属性的对象;如果一个对象被传入,它将被重用而不是创建一个新对象。

返回一个包含实体的连续碰撞检测边界矩形的对象。CCDBR包含了自上一帧以来实体的边界矩形的运动增量。如果实体只在最后一个坐标系中移动,那么CCDBR是最小的,但是如果它在两个轴上移动,它就包含一个非最小的区域。有关详细信息,请参阅FAQ#Tunneling.

注意:键有一个下划线前缀。这是由于x、y、w、h属性是setter和getter,用下划线(_x、_y、_w、_h)来包装底层属性。