Is either of these two ways of handling audio better?

Here is how Ben gets the thrust audio on the rocket ship to play when you press spacebar and then stop when you’re not pressing spacebar:

I stormed ahead and tried writing this bit before he did, creating my own separate function for handling audio:

Does anyone know if either way is better than the other? I assume later on he’s going to put it into a separate function, but I guess he’ll probably keep the audio.isPlaying check. Mine doesn’t have that, but it doesn’t feel like it needs it. Could I be opening myself up to future bugs by not having that check, or is mine actually just a more direct way of doing this?

Thanks!

Hi,

The former has the advantage of checking whether audio is already playing before trying to play it again which the latter lacks.

The name of the method in the latter is also a bit ambiguous, the name suggests it may process any sound but its in fact limited to the thrust.

Other than those observations, no real difference. :slight_smile:

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

Thanks! I guess I should do more checking if things get more complex.

And thanks for the comments. Feels obvious that I should’ve copy pasted the code instead…

1 Like

You’re very welcome and no problem, you know now for next time :slight_smile:

Privacy & Terms