@@ -15,6 +15,7 @@ var button_pressed := false:
1515 get :
1616 return main_button .button_pressed
1717var animation_running := false
18+ var audio_playing_at_frame := 0
1819
1920var audio_player : AudioStreamPlayer
2021@onready var properties : AcceptDialog = Global .control .find_child ("LayerProperties" )
@@ -68,20 +69,24 @@ func _ready() -> void:
6869
6970func _on_cel_switched () -> void :
7071 z_index = 1 if button_pressed else 0
71- var layer := Global .current_project .layers [layer_index ]
72+ var project := Global .current_project
73+ var layer := project .layers [layer_index ]
7274 if layer is AudioLayer :
7375 if not is_instance_valid (audio_player ):
7476 return
7577 if not layer .is_visible_in_hierarchy ():
7678 audio_player .stop ()
7779 return
7880 if animation_running :
79- var current_frame := Global . current_project .current_frame
81+ var current_frame := project .current_frame
8082 if (
8183 current_frame == layer .playback_frame
8284 or (current_frame == 0 and layer .playback_frame < 0 )
85+ ## True when switching cels while the animation is running
86+ or current_frame != audio_playing_at_frame + 1
8387 ):
8488 _play_audio (false )
89+ audio_playing_at_frame = current_frame
8590 else :
8691 _play_audio (true )
8792
@@ -124,6 +129,7 @@ func _play_audio(single_frame: bool) -> void:
124129 var frame_pos := frame .position_in_seconds (project , layer .playback_frame )
125130 if frame_pos >= 0 and frame_pos < audio_length :
126131 audio_player .play (frame_pos )
132+ audio_playing_at_frame = project .current_frame
127133 if single_frame :
128134 var timer := get_tree ().create_timer (frame .get_duration_in_seconds (project .fps ))
129135 timer .timeout .connect (func (): audio_player .stop ())
0 commit comments