Nek0 Posted September 5, 2021 Share Posted September 5, 2021 Wondering what the best way to play a sound effect in Phaser 2 is. So far I've been doing something like this: sfx = new Audio('sound1.mp3'); sfx.volume = .5; sfx.play() The same variable "sfx" gets used for each sound, it's just reassigned on the spot as 'sound2.mp3', 'sound3.mp3', etc. I'm sure this is the wrong way, but it does get it all done in a single line per sound effect. Nevertheless this method causes a small delay which causes the game to freeze for 1/10 of a second the first time it is played, which then causes objects to fall through floors occasionally. I looked at some of the sound examples, but the ones I saw were quite long and I'm wondering if there's a briefer solution to this. Link to comment Share on other sites More sharing options...
Nek0 Posted September 17, 2021 Author Share Posted September 17, 2021 bump Link to comment Share on other sites More sharing options...
Horizonicblue Posted September 21, 2021 Share Posted September 21, 2021 Here is a simple example to play sound in Phaser v2 https://phaser.io/examples/v2/audio/play-music I didn't understand why you want to use same variable for all sounds Nek0 1 Link to comment Share on other sites More sharing options...
Nek0 Posted September 22, 2021 Author Share Posted September 22, 2021 me neither =P i'm still rather new at this. thanks for the above advice though. it did point me in the right direction. implementing sound effects this way does make things run smoother. that example, however, shows how to play 1 music file 1 time. i'm looking to play multiple sound effects multiple times. so far the sfx run ok, but they don't seem to play over each other the way the old method did, i.e. explosions running through explode.mp3 being played 10 times in 1 second will only play the 10th sound effect all the way through, the rest get cut off because it seems to want to overwrite the previous one with the newer one each time. any way to play the full sound 10 times in rapid succession? Link to comment Share on other sites More sharing options...
Horizonicblue Posted September 22, 2021 Share Posted September 22, 2021 Check this example https://phaser.io/examples/v2/audio/audio-sprite which allows multiple sound on top of other. If I understood correctly, instead of adding audio multiple times you can add it once and then play it multiple times using same one variable. Nek0 1 Link to comment Share on other sites More sharing options...
Nek0 Posted September 22, 2021 Author Share Posted September 22, 2021 ah yes, that solves it. "soundeffect.allowMultiple = true" was the missing link. the odd thing is i recall looking at that exact example and even click tested the sounds and they DIDN'T stack. i must've been looking at the wrong VERSION of the example or something? anyway thanks again. as ever you're a lifesaver. Link to comment Share on other sites More sharing options...
Horizonicblue Posted September 24, 2021 Share Posted September 24, 2021 Glad to hear that it saved your life? maybe you clicked the button exactly when the audio ended, it happened to me few times Nek0 1 Link to comment Share on other sites More sharing options...
Nek0 Posted September 25, 2021 Author Share Posted September 25, 2021 no i pressed the longest one to be sure that wouldn't happen and it was still restarting. i think there's different versions of these example pages or something Link to comment Share on other sites More sharing options...
Recommended Posts