playing a sound issue

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By abdolilah

i have some coins in my game and i dont know how to play the coin sound
the coin is area2D and when i wright in the coin script to play the sound when it is colliding it dont play it , and when i connect the collison signal to a nod2D and tell it to play the sound it play the sound over and over .
so what should i do
hear is the project: https://drive.google.com/open?id=1YR-3ibVfqjFDmuM-Cu5xbMgU5wF8csPc

You should just copy your code here instead of sharing your project. Also , since you didn’t make your project downloadable

lowpolygon | 2019-08-08 05:39

:bust_in_silhouette: Reply From: MerlinoDreamLab
  1. Select the sound from the file system
  2. Select Import (close to the Scene TAB)
  3. Deselect Loop
  4. Click Reimport

thanks -----------

abdolilah | 2019-08-08 17:58

thanks MerlinoDreamLab !

uralys | 2021-11-22 23:42

:bust_in_silhouette: Reply From: Skipperro

I had same issue before. Don’t know it my solution is an official one, but I’ve noticed, that if a sound is in .ogg format, it loops until you manually stop it, but .wav sounds play only once.

So for background music I use .ogg and for effects, like your coin, I save sounds as .wav and it works as intended.

.wav files can also loop, it is just that by default they are imported as non-looping. You just need to select the file in godot, go to the import tab, select the loop option, then re-import.

Eric Ellingson | 2019-08-08 14:34

:bust_in_silhouette: Reply From: johnygames

I figured what the problem is and it has to do with this bit of code in the coin script:

func _on_Area2D_body_entered(body):
	print(body.name)
	a.play()
	scene_instance.sc+=10
	queue_free()
	pass # Replace with function body.

When you enter the area it starts to play the sound but immediately after you kill the coin node using queue_free(). So, the sound isn’t played at all. You will notice that if you remove this line, the coin will not disappear and the sound will play normally.
I suggest you place the sound somewhere else in your game, so that it is independent from other elements that might get destroyed in-game. Make a separate script, or put it your scene_instance singleton.

As for the looping sound, that’s an import property. Go to your sound file in the coin folder, select it and go to the Import tab (upper left corner of the editor, Godot 3.1 default layout). There you will see the Loop property ticked. Uncheck it and hit reimport. Problem solved!

Please upvote this answer and mark it as best if you found it useful, so that others can find it too.

1 Like