Tween is not working

Godot Version

v4.3.stable.steam [77dcf97d8]

Question

What is the difference between the following code?

screenshot

working

extends Node2D
@export var _sprImage: Sprite2D
@export var _btnStart: Button
var _tween: Tween
var _isChage: bool = false

func _ready() -> void:
	_btnStart.pressed.connect(on_start)

func on_start():
	_tween = create_tween()
	_tween.set_loops(-1)
	_tween.tween_property(_sprImage,'scale', Vector2(0,0), 1)
	_tween.tween_property(_sprImage,'scale', Vector2(1,1), 1)
	_tween.play()
	print_debug('start')

not working

extends Node2D
@export var _sprImage: Sprite2D
@export var _btnStart: Button
var _tween: Tween
var _isChage: bool = false

func _ready() -> void:
	_btnStart.pressed.connect(on_start)
	_tween = create_tween()

func on_start():
	_tween.set_loops(-1)
	_tween.tween_property(_sprImage,'scale', Vector2(0,0), 1)
	_tween.tween_property(_sprImage,'scale', Vector2(1,1), 1)
	_tween.play()
	print_debug('start')

_tween.play(), according to the docs, “Resumes a paused or stopped Tween”, so it actually is not needed in the first version. create_tween() creates a tween that “will start automatically on the next process frame or physics frame”, also according to the docs.

My assumption is that in the second version, your tween starts, has nothing to do, and ends instantly. When you press the button, you add some animation to the tween in on_start, but as you never called stop() on it, the tween’s state is not reset and it does nothing.

3 Likes

From the docs:

Note: Tweens are not designed to be re-used and trying to do so results in an undefined behavior. Create a new Tween for each animation and every time you replay an animation from start. Keep in mind that Tweens start immediately, so only create a Tween when you want to start animating.

2 Likes