javascript - 调用子类函数时调用父类(super class)函数

标签 javascript html inheritance canvas

我真的很难想出一个标题,但基本上我正在 html5 Canvas 上开发一个游戏,并且有一个名为 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";
}

最佳答案

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;

是错误的。他们错了,因为

  • 您正在将原型(prototype)设置为 Player 的实例
  • 每次创建 aiPlayer 的新实例时,您都会重置 aiPlayer 的原型(prototype)和原型(prototype)的构造函数。您应该将对原型(prototype)的所有修改移至构造函数之外,如下所示:

-

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() { 
    ....
}

设置子类原型(prototype)的正确方法是这样的

aiPlayer.prototype = Object.create(Player.prototype, {
    constructor : {
        value : aiPlayer
    }
};

这会将继承自 Player.prototype 的新对象设置为 aiPlayer 原型(prototype)(即以 Player.prototype 作为其原型(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 中)

关于javascript - 调用子类函数时调用父类(super class)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29056370/

相关文章:

html - 容器内相邻元素之间的一致间距

java - 不使用 super 的子类方法的默认行为是什么?

java - 实体 Sprite 怪物实体实例...就这么简单但不起作用?

javascript - JavaScript 中的凯撒密码 - 为什么 'A' 在这里变成 '['?

javascript - 在 Express JS 中设置 HTTPS

javascript - 设置 [CSS] :hover and :checked condition

java - instanceof 方法到底是如何工作的?为什么它在我的代码中不起作用,如下所示?

javascript - 这是顺序运行代码片段的好方法吗?

javascript - 映射后应用 knockout 扩展器

javascript - 在 AngularJS 中将数据从单击的项目传递到 Controller