2017-01-22 6 views
0

私は敵の2つのタイプを作成しようとしています。最初は睡眠とパトロールの2つの方法を持つロボットです。JavaScriptゲームの敵の継承 - フェイザーのフレームワーク

私の2番目の敵は飛行エネミーです。その目的は、ロボットからスリープ方法を継承するが、パトロール方法を修正することである。

飛行方法が改訂されている間に飛行しているエネミーがロボットからどのように継承されるのか誰にも見せてもらえますか?

以下は私のコードです。 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); 
    }; 

答えて

1

試してみてください。

//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でそれをやろうとすると同じですが、上書きするか、飛行に必要なメソッドのプロトタイプを継承します。この場合は、2:

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