+1 vote

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?

in Engine by (141 points)

1 Answer

+3 votes
Best answer

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:

enter image description here

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

animated_sprite.play("left")
by (28,833 points)
selected by

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"

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.