您好我有 3 个 javascript 文件:Game.js、Player.js、Drawable.js。现在在 Game.js 中,我想创建一个对象 _player,它是 Drawable,然后是 Player。这意味着 _player 是一个播放器对象,它是一个扩展 Drawable 的类。
可绘制的.js
function Drawable(x, y, src)
{
this.x = x;
this.y = y;
this.img = new Image();
this.img.src = src;
this.width = this.img.width;
this.height = this.img.height;
this.draw = function(canvas)
{
canvas.drawImage(this.img,this.x, this.y);
}
this.midpoint = function()
{
return {
x: this.x + this.width/2,
y: this.y + this.height/2};
}
}
}
播放器.js
function Player()
{
this.moveLeft = function()
{
this.x -= 3;
}
this.moveRight = function()
{
this.x += 3;
}
this.moveUp = function()
{
this.y -= 3;
}
this.moveDown = function()
{
this.y += 3;
}
}
游戏.js
var _player;
_player = new Player();
_player.draw(...);
_player.moveLeft();
...
...
这就是我想做的。我试图把 Player.prototype = new Drawable; 但它不起作用。我能怎么做?