在 godot 中出现音频问题,当我不想播放音频时

Having an audio issue in godot where audio plays when i dont want it to

我不擅长编码。我正在尝试制作一个在读出文本时播放音频的对话框。我已经让它运行良好,但我 运行 遇到了两个问题。第一个(最重要的)是每当我开始关卡时,声音都会在我没有完成条件(或者我真的不知道我在说什么)的情况下播放。第二个问题是,如果我也按 ui_accept(enter) fast/if 我会在对话框 done/sometimes 只是 运行domly 之前这样做,即使在对话框之后音频也会继续播放已关闭。这是我的狗屎代码

对话框代码

extends Control

var dialog = [
    'test1',
    'test2',
]
var dialog_index = 0
var finished = false
var test = false

func _ready():
    MusicController.dialogsfx.stop()
    load_dialog() 
    Global.DialogBox = self
    
func _exit_tree():
    Global.DialogBox = null


func _physics_process(delta):
    $"Ind".visible = finished
    if Input.is_action_just_pressed("ui_accept"):
        load_dialog()
    if test == true:
        if finished == false:
            MusicController.dialogsfx.play()
        if finished == true:
            MusicController.dialogsfx.stop()
    if Global.player.can_move:
        MusicController.dialogsfx.stop()

func load_dialog():
    if dialog_index < dialog.size():
        test = true
        finished = false
        $RichTextLabel.bbcode_text = dialog[dialog_index]
        $RichTextLabel.percent_visible = 0
        $Tween.interpolate_property(
            $RichTextLabel, "percent_visible",  0, 1, 1,
            Tween.TRANS_LINEAR, Tween.EASE_IN_OUT
        )
        $Tween.start()
        
        
    else:
        Global.npc1.dialogsoundstop = true
        MusicController.dialogsfx.stop()
        Global.player.can_move = true
        Global.npc1.done = true
        test = false
        print("test3")
        print("test4")

        queue_free()
        test = false
        
    dialog_index += 1
    
func _on_Tween_tween_completed(object, key):
    finished = true

音乐控制器代码

extends Node

var menu_music = load("res://mainmenutheme.wav")
var dialog_sfx = load("res://Sounds/dialogsound.wav")
onready var dialogsfx = $dialogsfx

func _ready():
    $dialogsfx.stream = dialog_sfx

func play_music():
    $Music.stream = menu_music
    $Music.play()


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#   pass

我认为您完全可以在 load_dialog 上完成此操作。当有文本行时开始音频:

func load_dialog():
    if dialog_index < dialog.size():
        # … some code …
        MusicController.dialogsfx.play()
    else:
        # … some code …
        
    dialog_index += 1

您也可以查看是否已经在播放,如果是,请不要再播放:

func load_dialog():
    if dialog_index < dialog.size():
        # … some code …
        if not MusicController.dialogsfx.playing:
            MusicController.dialogsfx.play()
    else:
        # … some code …
        
    dialog_index += 1

当不再有时,停止它:

func load_dialog():
    if dialog_index < dialog.size():
        # … some code …
        if not MusicController.dialogsfx.playing:
            MusicController.dialogsfx.play()
    else:
        # … some code …
        MusicController.dialogsfx.stop()
        
    dialog_index += 1

我不知道您是否希望音频在文本完成显示时停止(即补间完成时)。如果这样做,您可以将其添加到 _on_Tween_tween_completed:

func _on_Tween_tween_completed(object, key):
    # … some code …
    MusicController.dialogsfx.stop()

对了,你说加载关卡时会播放声音。可能是 AudioStreamPlayerautoplay 设置为 true。