【发布时间】:2015-03-15 01:45:44
【问题描述】:
我真的很难想出一个标题,但基本上我正在开发一个 html5 画布中的游戏,并且有一个名为 player 的类,它有一个子类 aiPlayer,用于对抗 ai。更新播放器的代码如下所示:
var entitiesCount = this.entities.length;
for (var i = 0; i < entitiesCount; i++) {
var entity = this.entities[i];
entity.update();
if (entity instanceof special && entity.x > WIDTH || entity.x + 200 < 0) {
this.entities.splice(i, 1);
entitiesCount--;
}
}
但是,aiPlayer 从不使用 aiPlayer 更新功能进行更新。我已经打印出每个实体的构造函数,并且有一个 Player 和一个 aiPlayer。但是,当打印出他们正在调用的方法时,他们都在调用 Player 更新。有谁知道它为什么会这样做? 此外,如果有帮助,aiPlayer 更新如下所示:
aiPlayer.prototype.update = function() {
if((this.game.timer.gameTime % this.moveTime) > (this.moveTime * 0.9)) {
this.chooseMove();
}
Player.prototype.update.call(this);
};
而ai构造函数看起来像:
function aiPlayer (game, character, x, y, health) {
Player.call(this, game, character, x, y, health, PLAYER2_CONTROLS, "left");
aiPlayer.prototype = new Player(this.game, this.character, this.x, this.y,
this.health, this.control, this.facing);
aiPlayer.prototype.constructor = aiPlayer;
this.controls = PLAYER2_CONTROLS;
this.attackLength = 50;
this.fleeLength = 70;
this.moveTime = 1;
this.prevControl = "idle";
}
【问题讨论】:
标签: javascript html inheritance canvas