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

Godot Version

4.1.1

Question

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.

Code:

func _process(delta):

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

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

func ChangeColor(color,timex):

var t = create_tween()
t.tween_property($Color,"modulate",color,timex)

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.kill()
    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.