Tween function - When a previous animation ends, interrupts the current animation

Godot Version



With the tween function I am interpolating colors to a sprite. The problem is that when I want to change to the green color and it was previously changing to red without the animation ending, the green color is interrupted and the sprite changes to red.


func _process(delta):

if Input.is_action_just_pressed("ui_left"):
	$Label.text = "Change to RED"

if Input.is_action_just_pressed("ui_right"):
	$Label.text = "Change to GREEN"

func ChangeColor(color,timex):

var t = create_tween()

I assume this is because the previous animation is still in progress and once it finishes, it replaces the current animation.
How can I solve this problem?

Yes, you’ll want to keep track of the previous tween you started so you can cancel it, and then you can call tween.kill()

something like

var tw_color: Tween = null

func change_color(to_color: Color, timex: float) -> void:
    if tw_color and tw_color.is_valid():
    tw_color = create_tween()
    tw_color.tween_property($Color, "modulate", to_color, timex)
1 Like

Thank you very much ^^

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.