How to loop a tween ? [SOLVED]

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By asetyowatir

Hello, I’m new here. I want to create a tween to loop forever, with this setting :

a sprite which it’s alpha will go back and forth from 0 to 1

Something like ping-pong / flip-flop looping.
I have tried making a 2 tween, one for 0 to 1 the other for 1 to 0. After that I have tried calling it with on tween complete to start each other but it just running once and done.

Someone please guide me trough this, thank you for reading.

1 Like
:bust_in_silhouette: Reply From: Footurist
extends Node2D

onready var tween_values = [0, 10]

func _enter_tree():
    var tween = Tween.new()
    add_child(tween)

func _ready():
    _start_tween()
    
func _start_tween():
    $Tween.interpolate_property(..., tween_values.[0], tween_values[1], ...)    
    $Tween.start()

func _on_tween_completed(object, key):
    tween_values.invert()
    _start_tween()

Or you could set up two tweens and make them call each other at the tween_completed signal. Connect the signal via the inspector tab “signals” or via code:

connect("tween_completed", self, "on_tween_completed")

That’s all. :stuck_out_tongue:

thank you very much! I should have learn array more

asetyowatir | 2018-02-26 03:46

It would’ve been a much more useful feature if tweens would have the “Tween.LOOP” trans stuff

kenoma | 2020-12-10 05:55

that’s genious

Natink26 | 2021-09-05 18:00

1 Like
:bust_in_silhouette: Reply From: igbow

animation player is your friend :slight_smile:

What happens when everything is dynamic? AnimationPlayer might not work

shakemakesgames | 2022-02-05 18:51

Sorry to necro, but it’s an XY problem. The first thing I though was looping tween for a certain animation, because I wanted easing. So I googled how to loop tweens in Godot. However, this answer reminded me about the AnimationPlayer, which is what I really needed. I can easily create easing with keyframes, looping is supported out of the box, and animations can be created declaratively in the editor, so no extra cluttering in my scripts.

dmitriy_shmilo | 2022-06-12 09:01