JavaScript游戏的敌人继承 - Phaser框架

问题描述:

我试图创造两种类型的敌人,第一种是有2种方法的机器人:睡眠和巡逻。JavaScript游戏的敌人继承 - Phaser框架

我的第二个敌人是飞行敌人。目的是继承机器人的睡眠方法,但修改巡视方法。

任何人都可以告诉我,我的飞天可以如何从机器人继承,同时修改巡逻方法吗?

以下是我的代码。当我创建飞行敌人时,其巡逻方法将覆盖机器人的巡逻方法,所有敌人都有相同的行为。

//-------------------------- 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); 
    }; 

尝试:

//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'); 
} 

如果问题仍然存在,这可能是由于这条线(我真的不知道):

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

如果继续它也试图用Sprite来做,它会覆盖或继承你在flyingEnemy中需要的方法的原型,在这种情况下它们只有2:

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