1
我正在制作一个我有物体移动的物体,每个物体都附有一个图像,但我不知道如何将它们从中心移动到左上角。如何在Javascript中获取图像的中心?
这是玩家:
function Player() {
this.height = 167.5;
this.width = 100;
this.pos_x = 350;
this.pos_y = 425;
this.player_image = new Image();
this.player_image.src = 'img/player_car_img.png';
};
及方法“移动”:
Player.prototype.move = function(){
if (37 in keysDown) {
this.pos_x -= 10;
} else if (39 in keysDown) {
this.pos_x += 10;
}
};
听起来不错,upvote。也许缓存'image.width/2'和'image.height/2',这样它们不会被每次绘制重新计算。 – markE
谢谢!这样可行 –