coin: Reparent AudioStreamPlayer rather than awaiting#79
Conversation
We want the "collected" sound effect to outlast the visible lifetime of the coin: the coin should disappear immediately, but the sound effect needs to keep playing until it's done. Previously this was implemented by having the coin disable collisions, hide itself, wait for the sound effect (if any) to finish playing, and only then free itself. Instead, we can reparent the AudioStreamPlayer to the coin's parent node, so that it will continue playing even after the coin is removed from the scene.
|
The question is: is this clearer than the previous code? I think yes - but if one wanted to add an animation, then one would have to restore the old monitoring/monitored logic anyway. |
|
Play this branch at https://endlessm.github.io/moddable-platformer/branches/endlessm/wjt/coin-reparent-audiostreamplayer. (This launches the game from the start, not directly at the change(s) in this pull request.) |
| if _collect_sound_player: | ||
| # Move the sound player node to the coin's parent. Otherwise, when the coin is freed, | ||
| # the sound player would also be freed and the sound would cut off. | ||
| _collect_sound_player.reparent(get_parent()) |
There was a problem hiding this comment.
Does the sound change its panning in the left/right speakers because of this? I haven't tried.
In case that happens, Node.reparent() has a second parameter keep_global_transform so you can pass True to it. Unless it doesn't matter because the sound is already playing!
There was a problem hiding this comment.
The second parameter defaults to True.
Indeed. Looking at the diff, disabling the collision seems a better option. When debugging collision shapes in the editor, they even change to a grayed-out color when disabled. |
|
I'll close this without merging. I wanted to see how it looked because I saw this technique in a video about sound effects and wondered if it would be an improvement. Neither one seems particularly elegant to me, and I think the current code leaves more options open for the curious learner. |
We want the "collected" sound effect to outlast the visible lifetime of the coin: the coin should disappear immediately, but the sound effect needs to keep playing until it's done.
Previously this was implemented by having the coin disable collisions, hide itself, wait for the sound effect (if any) to finish playing, and only then free itself.
Instead, we can reparent the AudioStreamPlayer to the coin's parent node, so that it will continue playing even after the coin is removed from the scene.