|Topic was automatically imported from the old Question2Answer platform.
In this project of mine, I have a gun and bullets programmed, so when I press the shoot button more than once, the audio starts from the beginning and there is no type of overlapping in the audio. how do i solve this?
I assume that each time you shoot, you’re calling
play() on a single
AudioStreamPlayer object. If that’s the case, the sound will start from the beginning for each shot - as you describe. And, since a single
AudioStreamPlayer can only play a single sound at a time, you won’t get any overlapping sounds. You have a few choices.
The simplest would be to (after each shot) check if your
AudioStreamPlayer is already palying a sound (from a previous shot). If it is, don’t play a new shot sound. That will at least allow the shot sound to complete instead of being “cut-off” and restarted by the next shot. You’d do that something like this:
# not currently playing a sound so play a new one
If you want to have multiple shot sounds playing at the same time, you’ll need to have a separate
AudioStreamPlayer for each sound that will play concurrenlty. While it’s not too difficult to create a basic audio manager that would spin up (and then pool for reuse) new
AudioStreamPlayer objects as needed, it’s definitely more complex than the above suggestion.