2013-03-09 69 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