A question about Tween in Godot4.3

Godot Version

4.3

Question

I created an animal scene, and I wanted the animal to move back and forth and play the corresponding left and right animations (with tween and AnimationPlayer)
The left and right moves succeeded, but only the left animation was played (the animation itself was fine), i.e. only the last line of code except for tween could be run(I have annotations in the code) :upside_down_face:
Here’s the code:
extends StaticBody2D
var tween:Tween
@onready var animation_player: AnimationPlayer = $AnimationPlayer
var x : bool

Called when the node enters the scene tree for the first time.

func _ready() → void:
tween = get_tree().create_tween().set_loops()
animation_player.play(“right”)#This line does not run!!!!
tween.tween_property(self,“position:x”,100,2).as_relative()
#Only this line can be run, and the code that plays the animation on the right cannot be run↓↓↓↓
animation_player.play(“left”) tween.tween_property(self,“position:x”,-100,2).as_relative()
func _process(delta: float) → void:
pass
(I don’t know much English, so I translated this question)HELP ME! :grinning: :stuck_out_tongue_winking_eye:

The play(“right”) line runs but you’re immediately calling play(“left”) so that’s what starts playing, you have to use the tween to control the animation player, instead of just calling play on it directly.
Try something like:

func _ready() -> void:
	tween = get_tree().create_tween().set_loops()
	tween.tween_callback(animation_player.play.bind("right"))
	tween.tween_property(self,"position:x",100,2).as_relative()
	tween.tween_callback(animation_player.play.bind("left"))
	tween.tween_property(self,"position:x",-100,2).as_relative()

1 Like

thank you very much :kissing:,i’m just have learned godot for 1 month(my english is not very good because i’m just 12) :wink:
this is my first question in this web,i get the anwser for the first time(quick) :grinning:

1 Like