我的实体应该朝着鼠标直线移动。它很接近,但还没有完全到那里。这是一个工作演示,向您展示我的意思。
这是一个屏幕截图:
红色代表鼠标所走的路径。如您所见,实体不采用相同的路径。
相关代码:
EntityPlayer = ig.Entity.extend({
movementspeed: 400,
update: function() {
this.parent();
this.move_toward_coord(ig.input.mouse.x, ig.input.mouse.y);
},
move_toward_coord: function(x, y) {
var distance_to_target_x = x - this.pos.x - this.size.x / 2;
var distance_to_target_y = y - this.pos.y - this.size.y / 2;
if(Math.abs(distance_to_target_x) > 1 || Math.abs(distance_to_target_y) > 1) {
this.vel.x = (distance_to_target_x > 1 ? 1 : -1) * this.movementspeed * (Math.abs(distance_to_target_x) / (Math.abs(distance_to_target_x) + Math.abs(distance_to_target_y)));
this.vel.y = (distance_to_target_y > 1 ? 1 : -1) * this.movementspeed * (Math.abs(distance_to_target_y) / (Math.abs(distance_to_target_x) + Math.abs(distance_to_target_y)));
} else {
this.vel.y = 0;
this.vel.x = 0;
}
}
});
我怀疑该move_to_coord
方法有问题,但是在调整了太多小时后,我仍然不确定它是什么...
船为什么不走直线?