【发布时间】:2013-03-13 10:21:07
【问题描述】:
我试图让我的画布游戏中的精灵不断向玩家移动,直到它发生碰撞。执行此操作的相关函数是 update() 函数:
Enemy.prototype.update = function(playerX, playerY) {
// Rotate the enemy to face the player
this.rotation = Math.atan2(this.y - playerY, this.x - playerX) - 2.35;
// Move in the direction we're facing
this.x += Math.sin(this.rotation) * this.speed;
this.y += Math.cos(this.rotation) * this.speed;
}
this.x、this.y、this.rotation和this.speed分别是敌人的X位置、Y位置、旋转和速度。
这有点用,但是敌人距离玩家大约 300 像素,然后开始向左转向并远离玩家,与它来向玩家的方向成 90 度角。
由于这有点难以解释,我录制了一个快速视频来帮助说明问题:http://www.screenr.com/AGz7
敌人是橙色精灵,玩家是白色精灵。
我为使敌人向玩家移动而进行的计算有什么问题?
【问题讨论】:
标签: javascript math canvas 2d-games