简体   繁体   English

JavaScript游戏敌人继承-Phaser框架

[英]JavaScript game enemy inheritance - Phaser framework

I'm trying to create two types of enemies, the first is a robot that has 2 methods: sleep and patrol. 我正在尝试创建两种类型的敌人,第一种是具有两种方法的机器人:睡眠和巡逻。

My second enemy is a flyingEnemy. 我的第二个敌人是飞行敌人。 The aim is to inherit the sleep method from the robot but amend the patrol method. 目的是继承机器人的睡眠方法,但修改巡逻方法。

Can anyone show me how my flyingEnemy can inherit from robot whilst amending the patrol method? 谁能告诉我我的flyingEnemy如何在修改巡逻方法的同时从机器人继承下来?

Below is my code. 下面是我的代码。 When I create the flyingEnemy, its patrol method overwrites the robot's patrol method and ALL enemies have the same behavior. 当我创建flyingEnemy时,其巡逻方法将覆盖机器人的巡逻方法,并且所有敌人的行为相同。

//-------------------------- ROBOT ENEMY 

var SuperSmash = SuperSmash || {};

SuperSmash.Enemy = function(game, x, y, key, velocity, tilemap, player) {
  Phaser.Sprite.call(this, game, x, y, key);

  this.game = game;
  this.tilemap = tilemap;
  this.player = player;

};

SuperSmash.Enemy.prototype = Object.create(Phaser.Sprite.prototype);
SuperSmash.Enemy.prototype.constructor = SuperSmash.Enemy;

SuperSmash.Enemy.prototype.update = function() {
  this.currentstate();
};

SuperSmash.Enemy.prototype.sleep = function() {
};

SuperSmash.flyingEnemy.prototype.patrol = function() {
  var direction
  if (this.body.velocity.x > 0) {
    this.scale.setTo(1, 1);
    direction = 1;
  } else {
    this.scale.setTo(-1, 1);
    direction = -1;
  }

  var nextX = this.x + direction * (Math.abs(this.width) / 2 + 1);
  var nextY = this.bottom + 1;
  var nextTile = this.tilemap.getTileWorldXY(nextX, nextY, this.tilemap.tileWidth,   this.tilemap.tileHeight, 'collisionlayer');

  if (!nextTile) {
    this.body.velocity.x *= -1;
  }
};



    // --------------------------- FLYING ENEMY

    var SuperSmash = SuperSmash || {};

    SuperSmash.flyingEnemy = function(game, x, y, key, velocity, tilemap, player) {
      SuperSmash.Enemy.call(this, game, x, y, key);

      this.game = game;
      this.tilemap = tilemap;
      this.player = player;

      this.animations.add("fly", [0]);
    };

    SuperSmash.flyingEnemy.prototype = Object.create(SuperSmash.Enemy.prototype);
    SuperSmash.flyingEnemy.prototype.constructor = SuperSmash.flyingEnemy;

    SuperSmash.Enemy.prototype.patrol = function() {
      "use strict";
       SuperSmash.game.physics.arcade.moveToObject(this, this.player, 200);
    };

Try: 尝试:

//Defining methods (Robot)
SuperSmash.Enemy.prototype.sleep = function() {
  //Code
  console.log('Call sleep() from Robot');
};

SuperSmash.Enemy.prototype.patrol = function() {
  //Code
  console.log('Call patrol() from Robot');
};

//In FlyingEnemy
SuperSmash.flyingEnemy.prototype.sleep = function() {
   //Inheriting the method Sleep
   SuperSmash.Enemy.prototype.sleep(this);
   console.log('Call sleep() from flyingEnemy');
}

SuperSmash.flyingEnemy.prototype.patrol = function() {
   //Override
   console.log('Call patrol() from flyingEnemy');
}

If the problem persists, it is probably due to this line (I'm not really sure): 如果问题仍然存在,则可能是由于以下原因(我不太确定):

SuperSmash.flyingEnemy.prototype = Object.create(SuperSmash.Enemy.prototype);

If it continues the same it tries to do it with Sprite and it overwrites or it inherits the prototypes of the methods that you need in flyingEnemy, in this case they are only 2: 如果继续执行相同的操作,则尝试使用Sprite进行操作并覆盖它,或者它继承flyingEnemy中所需的方法的原型,在这种情况下,它们仅是2:

SuperSmash.flyingEnemy.prototype = Object.create(Phaser.Sprite.prototype);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM