Jump to content

How to add sounds to Weapon?


PixelProgrammer
 Share

Recommended Posts

Currently, in my game. I've done the following 

if (player.fireButton.isDown) { //firing straight up
            player.weapon.fireAngle = Phaser.ANGLE_UP;
            player.weapon.fire();
            this.shootSfx.play();
        }

The issue with this is that when I hold down the `fireButton` the first millisecond of the sound is played on a loop. Basically, the sound just keeps resetting.

How do I set it up so that it has to play the full sound clip before starting again?

Also, are there any Phaser examples of sounds and weapons being used together? 

Link to comment
Share on other sites

For checking if a sound is playing; https://phaser.io/docs/2.6.2/Phaser.Sound.html#isPlaying. So you could possibly do ...

if (player.fireButton.isDown) { //firing straight up
    player.weapon.fireAngle = Phaser.ANGLE_UP;
    player.weapon.fire();
   
    if (!this.shootSfx.isPlaying) {
        this.shootSfx.play();
    }
}

You could also potentially use https://phaser.io/docs/2.6.2/Phaser.Weapon.html#onFire event to only play the sound when a bullet is successfully fired (even though you call fire() a bullet may not be fired due to timing, count limit, etc...)

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...