How to make an animation loop after another animation

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

I want to create an animation that puts the character in a certain position and then starts the loop, like from frame 0 to 4, then the loop goes from 5 until 9, without restarting from 0. I already tried creating two animations and creating another function with yield like the one below:

func _play_animation():

$Animation_swalking.play("start_walking")
yield ($Animation_swalking, "animation_finished")
$Animation_walking.play("walking")

func _physics_process(delta):
	if Input.is_action_pressed("right"):
		velocity.x = SPEED
		$Sprite.flip_h = false
		$Sprite.play("walking")

Where is _play_animation() called?

exuin | 2020-10-02 05:16

I forgot to call it in the exemple, but it is supposed to be called when you press a key, like “left” or “right”.

Eduardo_Soares | 2020-10-02 05:38

So $Animation_swalking and $Animation_walking are two different nodes?

exuin | 2020-10-02 05:52

Yes, they are

Eduardo_Soares | 2020-10-02 06:03

Why not use signals, rather than yields?
Maybe I don’t use yield as much as I should.

Lazarwolfe | 2020-10-02 06:05

If they’re two different nodes, then are they visible at the same time? You don’t hide either of them. Also, you should probably just use a signal instead of yield.

exuin | 2020-10-02 06:13

Could you give me an example in gdscript?

Eduardo_Soares | 2020-10-02 06:18

Not really. Are Sprite, Animation_swalking and Animation_walking all animatedsprites? Because sprites don’t have play methods and you can have more than one animation for an animatedsprite.

exuin | 2020-10-02 06:20

func _on_animation_animation_finished(anim): 
  if anim.name == "start_running":
    $Animation.play("running")

Also, I don’t really know if having two animation nodes will cause unexpected behavior, never really done it myself.

Lazarwolfe | 2020-10-02 06:21