AnimationPlayer & AnimatedSprite2D
Godot has two everyday tools for 2D animation, and most characters use both:
| Node | Animates | Typical use |
|---|---|---|
AnimatedSprite2D |
which frame of a flipbook is shown, from a SpriteFrames resource |
run cycles, idle loops, explosions: frame-by-frame pixel art |
AnimationPlayer |
any property of any node, plus method calls, audio and other animations, on a timeline | a coin bobbing, a door opening, a hit flash, cutscenes, UI transitions, hitbox timing |
AnimationTree |
blends and switches between an AnimationPlayer's animations with a state machine or blend spaces |
characters with many states (mostly 3D) |
AnimatedSprite2D and SpriteFrames
In the editor:
- Add an
AnimatedSprite2Dto your player scene. In the Inspector, click Sprite Frames > New SpriteFrames, then click it to open the SpriteFrames panel at the bottom. - Rename
defaulttoidle. Click Add frames from sprite sheet (the grid icon), pick your sheet, set the horizontal and vertical frame counts, select the frames and click Add. - Add more animations (
run,jump,fall) with the Add Animation button. Set each one's FPS and toggle Loop (on forrun, off forland). - Tick Autoplay on Load for
idle, or call$AnimatedSprite2D.play("idle")from code.
The node emits frame_changed when the shown frame changes, animation_looped each time a looping animation wraps, and animation_finished when a non-looping one reaches its last frame. Here a 4-frame run at 10 FPS plays for half a second, then a 3-frame, non-looping land at 20 FPS:
extends Node2D
# frames: 45
var sprite: AnimatedSprite2D
func _ready():
# Stand-in textures; in a project these are frames from your sprite sheet.
var tex := ImageTexture.create_from_image(Image.create(16, 16, false, Image.FORMAT_RGBA8))
var frames := SpriteFrames.new() # comes with an animation called "default"
frames.rename_animation("default", "run")
for i in 4:
frames.add_frame("run", tex)
frames.set_animation_speed("run", 10) # 10 frames per second
frames.set_animation_loop("run", true)
frames.add_animation("land")
for i in 3:
frames.add_frame("land", tex)
frames.set_animation_speed("land", 20)
frames.set_animation_loop("land", false)
print("animations: ", frames.get_animation_names())
sprite = AnimatedSprite2D.new()
sprite.sprite_frames = frames
add_child(sprite)
sprite.frame_changed.connect(func(): print(" ", sprite.animation, " frame ", sprite.frame))
sprite.animation_looped.connect(func(): print(" ", sprite.animation, " looped"))
sprite.animation_finished.connect(func(): print(" ", sprite.animation, " finished"))
sprite.play("run")
func _process(_delta):
if Engine.get_process_frames() == 30:
print("switching to land at game frame 30")
sprite.play("land")animations: ["land", "run"]
run frame 1
run frame 2
run frame 3
run looped
run frame 0
run frame 1
switching to land at game frame 30
land frame 0
land frame 1
land frame 2
land finished
animation_finished never fires for a looping animation, a common reason "my code after the attack animation never runs". Calling play() with the animation that is already playing does nothing, so it is safe to call play("run") every frame; it does not restart it.
Choosing the animation
Keep the "which animation?" decision in one small function that reads the body's state. Being a pure function, it's easy to test:
extends SceneTree
# Picks the animation from the body's state. Keeping this a pure function
# makes it easy to test, and the player script just calls it every frame.
static func pick_animation(velocity: Vector2, on_floor: bool) -> String:
if not on_floor:
return "jump" if velocity.y < 0.0 else "fall"
if absf(velocity.x) > 10.0:
return "run"
return "idle"
func _init():
var cases := [
[Vector2(0, 0), true],
[Vector2(150, 0), true],
[Vector2(5, 0), true],
[Vector2(-150, -300), false],
[Vector2(-150, 120), false],
]
for c in cases:
var vel: Vector2 = c[0]
var anim := pick_animation(vel, c[1])
var flip := vel.x < 0.0 # face left when moving left
print(vel, " on_floor=", c[1], " -> ", anim, ", flip_h=", flip)
quit()(0.0, 0.0) on_floor=true -> idle, flip_h=false
(150.0, 0.0) on_floor=true -> run, flip_h=false
(5.0, 0.0) on_floor=true -> idle, flip_h=false
(-150.0, -300.0) on_floor=false -> jump, flip_h=true
(-150.0, 120.0) on_floor=false -> fall, flip_h=true
In the player script, after move_and_slide():
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
func _update_animation() -> void:
sprite.play(pick_animation(velocity, is_on_floor()))
if velocity.x != 0.0:
sprite.flip_h = velocity.x < 0.0 # keep the last facing when standing stillThe small dead zone (> 10.0) stops the sprite flickering between idle and run while the character decelerates.
AnimationPlayer: animate anything
An AnimationPlayer holds Animation resources. Each animation has tracks; each track targets a node path plus property (Coin:position:y) and holds keys (time → value) that are interpolated. Other track types call methods, play audio, or play other animations. In the editor:
- Add an
AnimationPlayerto the scene. The Animation panel opens at the bottom. - Click Animation > New, name it
collect, and set its length (the field next to the clock icon) to 0.5 s. - Select the
Coinnode. Every animatable property in the Inspector now has a key icon: set a value, click the key to insert it at the current time, move the time cursor, change the value, key it again. - For a method call, click Add Track > Call Method Track, pick the node, then right-click on the track at a time and choose the method.
- Press the play button in the panel to preview it. In code:
$AnimationPlayer.play("collect").
The same animation built in code, so you can see what the editor creates. The coin bobs up and back down while fading out, and a method track calls play_sound halfway:
extends Node2D
# frames: 40
var coin: Node2D
var player: AnimationPlayer
func _ready():
coin = Node2D.new()
coin.name = "Coin"
add_child(coin)
var anim := Animation.new()
anim.length = 0.5
# Track 0: Coin's position.y goes 0 -> -12 -> 0
var bob := anim.add_track(Animation.TYPE_VALUE)
anim.track_set_path(bob, "Coin:position:y")
anim.track_insert_key(bob, 0.0, 0.0)
anim.track_insert_key(bob, 0.25, -12.0)
anim.track_insert_key(bob, 0.5, 0.0)
# Track 1: Coin fades out
var fade := anim.add_track(Animation.TYPE_VALUE)
anim.track_set_path(fade, "Coin:modulate:a")
anim.track_insert_key(fade, 0.0, 1.0)
anim.track_insert_key(fade, 0.5, 0.0)
# Track 2: call a method on Main at 0.25 s
var calls := anim.add_track(Animation.TYPE_METHOD)
anim.track_set_path(calls, ".")
anim.track_insert_key(calls, 0.25, {"method": "play_sound", "args": ["coin.wav"]})
var library := AnimationLibrary.new()
library.add_animation("collect", anim)
player = AnimationPlayer.new()
player.root_node = NodePath("..") # paths are relative to this node's parent (Main)
add_child(player)
player.add_animation_library("", library) # "" = the default library
player.animation_finished.connect(func(anim_name): print("finished: ", anim_name))
player.play("collect")
func play_sound(file: String):
print("method track: play_sound(", file, ") at t = ", snappedf(player.current_animation_position, 0.01))
func _process(_delta):
if player.is_playing() and Engine.get_process_frames() % 6 == 0:
print("t = ", snappedf(player.current_animation_position, 0.01), ": y = ", snappedf(coin.position.y, 0.01),
", alpha = ", snappedf(coin.modulate.a, 0.01))t = 0.0: y = 0.0, alpha = 1.0
t = 0.08: y = -4.0, alpha = 0.83
t = 0.18: y = -8.8, alpha = 0.63
method track: play_sound(coin.wav) at t = 0.25
t = 0.28: y = -10.4, alpha = 0.43
t = 0.38: y = -5.6, alpha = 0.23
t = 0.48: y = -0.8, alpha = 0.03
finished: collect
The values are linear interpolations between keys: at t = 0.08 (5/60 s) y is -12 × 0.0833 / 0.25 = -4. Each key can use another transition curve, and each value track can switch to Cubic interpolation for smoother motion.
Useful details:
- Animations live in libraries. The default library has the empty name, so
play("collect")finds it; a library namedenemyis played withplay("enemy/attack"). root_node(default.., the player's parent) is where track paths start. If you move theAnimationPlayerin the tree, its tracks break; keep it as a direct child of the scene root.player.advance(seconds)jumps the animation forward immediately.play_backwards(),speed_scaleandqueue("next")cover most other needs.- Method tracks are perfect for gameplay timing: enable a hitbox at the exact frame of a sword swing, spawn a footstep dust cloud, emit a signal.
- Animating a sprite sheet with an
AnimationPlayer(a track onSprite2D.frame) is an alternative toAnimatedSprite2Dthat keeps frames, hitboxes and sounds on one timeline.
For a one-off effect created from code (a flash, a pop), a Tween is shorter than building an Animation: see Timers & Tweens.
Test yourself
Your attack animation loops, and the code connected to animation_finished never runs. Why?
animation_finishedis only for AnimationPlayer- Looping animations never finish; they emit
animation_loopedinstead - The signal must be connected in the editor
play()must be called every frame
What does $AnimatedSprite2D.play("run") do when run is already playing?
- Restarts it from frame 0
- Nothing; it keeps playing
- Throws an error
- Plays it backwards
Which is the best tool to enable a sword's hitbox at exactly 0.2 s into an attack?
- A Timer started in
_ready - A method or property track in the attack's
AnimationPlayeranimation _processcounting frames- An Area2D signal