调用子类函数时调用的超类函数
superclass function being called when subclass function is invoked
我真的很努力想出一个标题,但基本上我正在 html5 画布中开发一款游戏,并在与 ai 对战时有一个叫做 player 的类,带有一个子类 aiPlayer。更新播放器的代码如下所示:
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更新功能进行更新。我已经打印出了每个实体的构造函数,有一个播放器和一个 aiPlayer。但是,在打印出他们正在调用的方法时,他们都在调用播放器更新。有谁知道它为什么要这样做?此外,如果有帮助,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";
}
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";
}
这些行在这里
aiPlayer.prototype = new Player(this.game, this.character,
this.x, this.y,this.health,
this.control, this.facing);
aiPlayer.prototype.constructor = aiPlayer;
错了。他们错了,因为
- 您正在将原型设置为
Player
的实例 - 每次创建
aiPlayer
的新实例时,您都会重置aiPlayer
原型的原型和构造函数。您应该将所有修改移动到构造函数之外的原型,如下所示:
-
function aiPlayer (game, character, x, y, health) {
Player.call(this, game, character, x, y, health, PLAYER2_CONTROLS, "left");
this.controls = PLAYER2_CONTROLS;
this.attackLength = 50;
this.fleeLength = 70;
this.moveTime = 1;
this.prevControl = "idle";
}
aiPlayer.prototype.someMethod = function someMethod() {
....
}
设置子类原型的正确方法是这样的
aiPlayer.prototype = Object.create(Player.prototype, {
constructor : {
value : aiPlayer
}
};
这将aiPlayer
原型设置一个新对象,该对象继承自Player.prototype
(即将Player.prototype
作为其原型),并aiPlayer
注册为其构造函数
此外,Player
的.update
是从aiPlayer
调用的,因为您在此处显式调用它
aiPlayer.prototype.update = function() {
if((this.game.timer.gameTime % this.moveTime) > (this.moveTime * 0.9)) {
this.chooseMove();
}
Player.prototype.update.call(this); //you call the Player.update()
};
考虑到上述情况,您应该如何注册aiPlayer.update
aiPlayer.prototype = Object.create(Player.prototype, {
constructor : {
value : aiPlayer
}
};
aiPlayer.prototype.update = function update() {
//your code here
}
现在,当您创建新的aiPlayer
对象实例时,继承链将如下所示
aiPlayerInstance --> aiPlayer.prototype --> Player.prototype
当你调用aiPlayerInstance.update()
时,它将首先查看aiPlayer.prototype
,并且由于aiPlayer.prototype
确实有一个名为update
的方法,它将执行它,并且它不会在继承链中查看任何更远的位置(即在Player.prototype
相关文章:
- 调用子类函数时调用的超类函数
- 从单独的类调用活动函数
- 如何在singleton类中调用同级函数
- 从obj c类调用javascript函数对我不起作用
- 从“”调用构造函数;摘要“;基本“;类”;
- 我们可以在javascript函数中调用类属性吗
- JavaScript es6 从另一个类调用静态函数
- 在forEach中调用类函数:Javascript如何处理“this”关键字
- 动态调用类函数
- 如何从闭包调用javascript类函数
- 通过引用调用原型函数时,类会丢失“this”作用域
- 从构造函数调用类方法
- 从Javascript中的类中调用类函数,Jquery
- 从 JavaScript 调用类文件(.cs 在 C# 中)函数,并帮助编写 Ajax+Webservices
- 类函数调用
- 将文字/对象/类传递给具有数千次调用的函数的内存含义
- Angular 2 不能调用类函数
- 在javascript中的另一个类函数中调用类函数
- Mootools——从绑定的css类调用类函数,结果为'错误
- 从构造函数调用类函数