如何杀死击中敌人的武器子弹?移相器 2.7.5
How to kill a Weapon bullet that hit an enemy? Phaser 2.7.5
大家好,我正在制作这款简单的游戏,其中包含竞技场中的两名玩家
每个玩家都有一个可以发射弹丸的武器。
我想要的是,每当射弹击中一个方块或一个敌人时,它的 hp 就会减少 1,并且碰到敌人或方块的子弹会消失。
我试过使用Lifespan来杀死子弹(当子弹与敌人重叠时lifespan = 0)但没有成功。
我正在使用 bullet.kill()
,但它似乎也不起作用。
我是如何制作武器的:
weapon = game.add.weapon(10, 'bullet');
weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; weapon.bulletSpeed = 200;
weapon.fireRate = 1000;
weapon.trackSprite(this.player, 0, 0, false);
weapon2 = game.add.weapon(10, 'bullet');
weapon2.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
weapon2.bulletSpeed = 200;
weapon2.fireRate = 1000;
weapon2.trackSprite(this.player2, 0, 0, false);
碰撞:
game.physics.arcade.overlap(weapon.bullets, this.player2, this.hitP2, null, this);
game.physics.arcade.overlap(weapon.bullets, this.blocks, this.hitBlock1, null, this);
game.physics.arcade.overlap(weapon2.bullets, this.player, this.hitP1, null, this);
game.physics.arcade.overlap(weapon2.bullets, this.blocks, this.hitBlock2, null, this);
命中函数:
hitBlock: function(bul, blk){
bul.kill();
},
hitP: function(bul, enm){
// sfx de acerto
p1HP -= 1;
this.txtP1HP.text = 'P1 HP: ' + p1HP;
bul.kill();
if(p1HP < 1){
this.player.kill();
}, this);
现在发生的事情是,当子弹击中敌人时他会变得隐身并且子弹仍然 运行 直到出界。
为什么kill方法在这个中不能正常工作?
为什么敌人会隐形?
我不知道 Phaser,但我很感兴趣,所以,通过检查 the overlap docs 它指出:
The two objects will be passed to this function in the same order in which you specified them, unless you are checking Group vs. Sprite, in which case Sprite will always be the first parameter.
你显然声明了与一个组的重叠,所以你需要交换你的参数,精灵总是第一个参数。玩家消失是因为你杀死了它而不是子弹:
hitBlock: function(blk, bul){ //instead of (bul, blk)
bul.kill();
},
hitP: function(enm, bul){ //instead of (bul, enm)
// sfx de acerto
p1HP -= 1;
this.txtP1HP.text = 'P1 HP: ' + p1HP;
bul.kill();
if(p1HP < 1){
this.player.kill();
}
}
虽然我不确定它是否会有效地杀死子弹,因为第二个参数可能是一个子弹组而不是有效重叠的那个。
希望对您有所帮助!
大家好,我正在制作这款简单的游戏,其中包含竞技场中的两名玩家 每个玩家都有一个可以发射弹丸的武器。 我想要的是,每当射弹击中一个方块或一个敌人时,它的 hp 就会减少 1,并且碰到敌人或方块的子弹会消失。
我试过使用Lifespan来杀死子弹(当子弹与敌人重叠时lifespan = 0)但没有成功。
我正在使用 bullet.kill()
,但它似乎也不起作用。
我是如何制作武器的:
weapon = game.add.weapon(10, 'bullet');
weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; weapon.bulletSpeed = 200;
weapon.fireRate = 1000;
weapon.trackSprite(this.player, 0, 0, false);
weapon2 = game.add.weapon(10, 'bullet');
weapon2.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
weapon2.bulletSpeed = 200;
weapon2.fireRate = 1000;
weapon2.trackSprite(this.player2, 0, 0, false);
碰撞:
game.physics.arcade.overlap(weapon.bullets, this.player2, this.hitP2, null, this);
game.physics.arcade.overlap(weapon.bullets, this.blocks, this.hitBlock1, null, this);
game.physics.arcade.overlap(weapon2.bullets, this.player, this.hitP1, null, this);
game.physics.arcade.overlap(weapon2.bullets, this.blocks, this.hitBlock2, null, this);
命中函数:
hitBlock: function(bul, blk){
bul.kill();
},
hitP: function(bul, enm){
// sfx de acerto
p1HP -= 1;
this.txtP1HP.text = 'P1 HP: ' + p1HP;
bul.kill();
if(p1HP < 1){
this.player.kill();
}, this);
现在发生的事情是,当子弹击中敌人时他会变得隐身并且子弹仍然 运行 直到出界。
为什么kill方法在这个中不能正常工作? 为什么敌人会隐形?
我不知道 Phaser,但我很感兴趣,所以,通过检查 the overlap docs 它指出:
The two objects will be passed to this function in the same order in which you specified them, unless you are checking Group vs. Sprite, in which case Sprite will always be the first parameter.
你显然声明了与一个组的重叠,所以你需要交换你的参数,精灵总是第一个参数。玩家消失是因为你杀死了它而不是子弹:
hitBlock: function(blk, bul){ //instead of (bul, blk)
bul.kill();
},
hitP: function(enm, bul){ //instead of (bul, enm)
// sfx de acerto
p1HP -= 1;
this.txtP1HP.text = 'P1 HP: ' + p1HP;
bul.kill();
if(p1HP < 1){
this.player.kill();
}
}
虽然我不确定它是否会有效地杀死子弹,因为第二个参数可能是一个子弹组而不是有效重叠的那个。
希望对您有所帮助!