2013-03-09 66 views
0

精靈座標我有一個表示子彈及其基本實現一個精靈如下:更新基於其方向

function Bullet(x, y, rotation) { 
    this.x = x; 
    this.y = y; 
    this.direction = rotation; 
    this.speed = 5; 
} 

Bullet.prototype.update = function() { 
    // Move the bullet forward 
    this.x = Math.sin(this.rotation) * this.speed; 
    this.x = Math.cos(this.rotation) * this.speed; 
} 

我想要做的,是在前進的子彈它面對的方向和相對於它的速度。但是,當撥打update()方法this.xthis.x時是NaN

如果給出它的xyrotation信息,使精靈朝向它所面對的方向移動的正確方法是什麼?

+0

嘗試推行'VECTOR'類。它會使這件事更容易處理。 – Blender 2013-03-09 21:14:47

回答

1

你有一個錯字。這:

this.x = Math.sin(this.rotation) * this.speed; 

應該

this.x = Math.sin(this.direction) * this.speed; 
+0

對,這也解決了這個問題。 – dfsq 2013-03-09 21:14:47

+0

Woops,我改變了變量名並錯過了它。雖然它仍然無法正常工作,但子彈不會從它們的x和y位置移動。 :( – 2013-03-09 21:20:12

+0

沒關係,需要做'+ ='。 – 2013-03-09 21:22:26