msqar Posted February 9, 2018 Share Posted February 9, 2018 I wanted to try to access an audio file that was previously loaded during preloader process. But i couldn't seem to find a way. I was expecting something like "game.sound.getInstance('key')" but nothing like that. According to the Docs, the only way to get the instance in return is by first playing it. But that doesn't make sense to me, for example... what if i want to check if that audio file is already playing? If it's playing, don't re-play. Like this: public static playSound(game: Phaser.Game, soundName: string): void { var soundInstance = game.sound.getInstance(soundName); if (!soundInstance.isPlaying) { soundInstance.play(); } } The reason i'm doing this is because i play a "running" sound when my character runs towards left/right. This is all being handled inside the update() method. Hence, if i keep holding left/right arrows, the sound will play a trillion times per second instead of waiting the sound to finish and play whenever possible. If someone tell me how to avoid this without having to get the instance and stuff, it would be helpful, i just don't know what other workaround to try tbh. Thanks in advance. Link to comment Share on other sites More sharing options...
samme Posted February 10, 2018 Share Posted February 10, 2018 You should probably save your own reference and check that. You can use game.add.sound or game.sound.add. The Sound Manager doesn't have a method like this because it lets you add several sounds with the same key (that can be played at different times). msqar 1 Link to comment Share on other sites More sharing options...
msqar Posted February 10, 2018 Author Share Posted February 10, 2018 6 minutes ago, samme said: You should probably save your own reference and check that. You can use game.add.sound or game.sound.add. The Sound Manager doesn't have a method like this because it lets you add several sounds with the same key (that can be played at different times). Kk thanks Samme, you always help i ended up adding the files on my Player.ts file, the 2 sounds it uses for now, and then i just call the play and that's it. I will just keep it that way, add the sounds on the files it's going to be needed/used. Sorry if i bother again but i got a weird bug happening in my game related to collision detection, if you can pass by and check it out i'd really appreciate. Or maybe it happened to you too. This is the link to the post Link to comment Share on other sites More sharing options...
Recommended Posts