0 votes

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

asked Aug 7, 2019 in Engine by abdolilah (57 points)

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

3 Answers

0 votes
Best answer

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.

answered Aug 8, 2019 by johnygames (1,574 points)
selected Aug 8, 2019 by abdolilah
0 votes

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.

answered Aug 8, 2019 by Skipperro (125 points)

.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.

0 votes
  1. Select the sound from the file system
  2. Select Import (close to the Scene TAB)
  3. Deselect Loop
  4. Click Reimport

enter image description here

answered Aug 8, 2019 by MerlinoDreamLab (14 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.