嗨我有3個javascript文件:Game.js,Player.js,Drawable.js。 現在在Game.js中,我想要創建一個對象_player,即Drawable,而不是Player。這意味着_player是一個Player對象,這是一個EXTENDS可繪製的類。Javascript繼承3個分隔文件
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};
}
}
}
Player.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;
}
}
Game.js
var _player;
_player = new Player();
_player.draw(...);
_player.moveLeft();
...
...
這就是我想要的去做。我試圖把Player.prototype = new Drawable;但它不起作用。我能怎麼做?