How do I change the SpriteFrames loaded in AnimatedSprite using GDScript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By user41420082
:warning: Old Version Published before Godot 3 was released.

I want to swap out the SpriteFrames loaded onto my AnimatedSprite to indicate where the player’s direction is. I made 4 different SpriteFrames saved as *.tres and on the source folder. How do I swap them out on runtime using GDScript?

:bust_in_silhouette: Reply From: Zylann

In GDScript, if you want to change the SpriteFrames resource of an AnimatedSprite, you can do like this:

var frames = preload("path/to/your_sprite_frames.tres")
animated_sprite.set_sprite_frames(frames)

However I think you don’t need to make 4 SpriteFrames resources for each direction. I had a look at the editor, and realized it works like AnimationPlayer. You can have multiple sprite sequences in a single SpriteFrames, like this:

With this other technique you can play any of these by name in GDScript:

animated_sprite.play("left")

Oh wow, how did I not think of that. Thanks! I even have a walk and run set and never bothered to think “hmm… maybe I can add the 4 directions here too”

user41420082 | 2016-10-08 00:34