You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot脚本中coin.wav无法播放问题求助

金币收集音效无法播放的解决办法

问题根源

你在调用$coin_sound.play()之后马上执行了queue_free(),这会直接把当前的金币节点(包括它的子节点coin_sound)销毁掉——音效刚要启动就没了载体,自然听不到声音。

三种可行解决方案

1. 等音效播完再销毁节点

用定时器等待音效的完整时长,之后再销毁金币节点:

extends Area2D

signal coin_collected

func _on_coin_body_entered(_body):
    emit_signal("coin_collected")
    set_collision_mask_bit(0, false)
    print("collected")
    $coin_sound.play()
    print("started sound")
    # 等待音效播放完毕,get_length()会自动获取音效的时长
    await get_tree().create_timer($coin_sound.stream.get_length()).timeout
    queue_free()

2. 监听音效播放完成的信号

给AudioStreamPlayer绑定finished信号,等音效播完再触发销毁:

extends Area2D

signal coin_collected

func _on_coin_body_entered(_body):
    emit_signal("coin_collected")
    set_collision_mask_bit(0, false)
    print("collected")
    $coin_sound.connect("finished", self, "_on_sound_finished")
    $coin_sound.play()
    print("started sound")

func _on_sound_finished():
    queue_free()

3. 把音效节点移到不会被销毁的地方

比如把coin_sound移到主场景节点下(比如/root/Main),这样即使金币节点被销毁,音效节点还在,能正常播放:

extends Area2D

signal coin_collected

func _on_coin_body_entered(_body):
    emit_signal("coin_collected")
    set_collision_mask_bit(0, false)
    print("collected")
    # 替换成你实际的主节点路径
    get_node("/root/Main/coin_sound").play()
    print("started sound")
    queue_free()

额外注意

  • 记得把coin_sound的autoplay关掉,避免自动播放和代码调用冲突
  • 检查音效节点的音量设置,确保没被静音

内容的提问来源于stack exchange,提问作者user18513298

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 11:46:07