我正在尝试使用武器类,我发现了一些问题。
这是我的代码:
var game = new Phaser.Game(640, 360, Phaser.AUTO);
var GameState = {
preload: function(){
game.load.image('player', 'assets/images/player.png');
game.load.image('enemyBody', 'assets/images/enemyBody.png');
game.load.image('bullet', 'assets/images/bullet.png');
},
create: function(){
this.player = game.add.sprite(game.world.centerX, game.world.centerY, 'player');
this.enemy = game.add.sprite(500, 200, 'enemyBody');
//this.makeShoot();
this.enterKey = this.game.input.keyboard.addKey(Phaser.Keyboard.ENTER);
this.enterKey.onDown.add(this.pressEnterKey, this);
},
pressEnterKey: function(){
this.makeShoot();
},
makeShoot: function(){
this.player.rotation = this.game.physics.arcade.angleBetween(this.player, this.enemy);
this.bullet = game.add.weapon(1, 'bullet');
this.bullet.trackSprite(this.player, 0, 0, true);
this.bullet.fire();
}
};
game.state.add('GameState', GameState);
game.state.start('GameState');
当代码执行时,在第一次按下Enter后,子弹出现在(0,0),然后子弹出现在玩家位置。
如果我从注释行中删除评论,它将完美无缺。
我的错误在哪里?
答案 0 :(得分:0)
武器和子弹是两个不同的东西。您需要在create()
方法中初始化武器实例,然后在makeShoot()
方法中生成项目符号,如下所示:
create: function(){
this.player = ...
this.weapon = this.add.weapon(1, 'bullet');
this.weapon.trackSprite(this.player, 0, 0, true);
...
},
makeShoot: function(){
this.player.rotation = this.physics.arcade.angleBetween(this.player, this.enemy);
this.weapon.fire();
}