Gridwright Store

27 Nov 2026 · By Gridwright Studio

AnimatedSprite2D vs AnimationPlayer in Godot 4 for Pixel Art

Gridwright pack artwork for AnimatedSprite2D vs AnimationPlayer in Godot 4 for Pixel Art

Your knight's swing looks right, but the enemy loses health the moment the attack starts, or twice per swing, or from behind when the knight faces left. Godot 4 has two nodes that play a sprite sheet, AnimatedSprite2D and AnimationPlayer, plus AnimationTree on top of the second, and where the hitbox gets switched depends on which one you use.

Everything below was run in Godot 4.7.2 at a fixed 60 ticks a second, on the Knight from our Side-View Characters, whose JSON gives the attack's hit frame and hitbox, and on the Knight from Isometric Characters for 8 directions.

The short answer

  • AnimatedSprite2D when the animation is only pictures: a SpriteFrames holds each animation's frames, speed and loop, and play("walk") is all the code.
  • AnimationPlayer when the same timeline has to move other things too: a hitbox that is on for one frame, a sound, an arrow that leaves on the release frame.
  • AnimationTree, on top of an AnimationPlayer, when the rules for what plays next get long.

Many games use both: AnimatedSprite2D for coins and simple enemies, AnimationPlayer for the player and bosses.

The ten Side-View Characters on a platformer level: the knight's sword hits a skeleton, an archer shoots and a mage casts a fireball
Side-View Characters: 10 characters, 8 animations each, facing left and right, 32x32.

AnimatedSprite2D: frames, speed and two signals

The knight's sheet is 6 columns by 16 rows of 32x32 frames. Its JSON gives every animation's rectangles, fps and loop: idle is 4 frames at 6 fps and loops, attack is 5 frames at 12 fps and plays once.

Two signals do the work. animation_finished fires when an animation that doesn't loop ends: once for the attack in our run, never for idle, which sent animation_looped instead. frame_changed fires on every new frame, and that is where the hitbox goes.

The JSON says the attack hits on frame 2 (counted from 0), with the box around the sword at x 20, y 5, 12 by 27 pixels. With the sprite's Centered off and Offset at (-16, -32), the feet sit on the node's origin, so the box becomes a 12x27 RectangleShape2D at (10, -13.5) in an Area2D named Hitbox. Here is the knight's whole script, one item per line of the file:

  • extends CharacterBody2D
  • const HIT_FRAME := 2
  • @onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
  • @onready var hitbox: Area2D = $Hitbox
  • @onready var hitbox_shape: CollisionShape2D = $Hitbox/CollisionShape2D
  • func _ready() -> void: hitbox_shape.disabled = true; sprite.frame_changed.connect(_on_frame_changed); sprite.animation_finished.connect(_on_animation_finished); sprite.play("idle")
  • func face(dir: float) -> void: sprite.flip_h = dir < 0.0; hitbox.scale.x = -1.0 if dir < 0.0 else 1.0
  • func attack() -> void: sprite.play("attack")
  • func _on_frame_changed() -> void: hitbox_shape.disabled = not (sprite.animation == &"attack" and sprite.frame == HIT_FRAME)
  • func _on_animation_finished() -> void: if sprite.animation == &"attack": sprite.play("idle")

At 12 fps a frame lasts 5 ticks, and the box was on for exactly those 5 ticks, all on frame 2. An enemy hurtbox in front of the knight counted one hit per swing. The pack's left frames are its right frames mirrored (we compared all 34 pairs) and the cell is centred on the feet, so flip_h gives the same picture. Scale -1 on the Hitbox moves the box to x 0, where the JSON puts it for the left side, and after face(-1.0) only the enemy behind the knight was hit. More in how to flip a sprite in Godot 4 without it jumping sideways.

AnimationPlayer: key the hitbox on the timeline

Here the sprite is a plain Sprite2D with Hframes 6 and Vframes 16, and each animation keys its frame property. The attack is 0.4167 s long with keys for frames 30 to 34, one every 1/12 s. The hitbox needs no code: add a track for Hitbox/CollisionShape2D:disabled and key it true at 0, false at 0.1667 s (frame 2) and true again at 0.25 s. Three things we measured:

  • Set the frame track's Update Mode to Discrete. With Continuous, Godot rounded the frame number between keys, so each frame came in half a frame early: at 0.05 s it already showed frame 31, which Discrete showed from 0.083 s.
  • Key disabled = true at 0 in every other animation, idle included. Started in the middle of the hit frame, an idle without that key left the box on for good, and our enemy also took a hit the moment the knight appeared, before any attack. One key at 0 in idle fixed both.
  • A Call Method track does the same for the archer's arrow (spawn_frame 3 in its JSON, 0.25 s at 12 fps). Our test key called release_arrow() once per attack.

The script gets shorter:

  • extends CharacterBody2D
  • signal arrow_released
  • @onready var sprite: Sprite2D = $Sprite2D
  • @onready var hitbox: Area2D = $Hitbox
  • @onready var anim: AnimationPlayer = $AnimationPlayer
  • func _ready() -> void: anim.animation_finished.connect(_on_animation_finished); anim.play("idle")
  • func face(dir: float) -> void: sprite.flip_h = dir < 0.0; hitbox.scale.x = -1.0 if dir < 0.0 else 1.0
  • func attack() -> void: anim.play("attack")
  • func release_arrow() -> void: arrow_released.emit()
  • func _on_animation_finished(anim_name: StringName) -> void: if anim_name == &"attack": anim.play("idle")

The box was on for the same 5 ticks, on sheet frame 32, with one hit per swing on the side the knight faced. The pack's hurt animation already starts on a white flash frame; to flash any sprite in code, see hit flash in Godot 4.

AnimationTree: let a state machine pick

Once a character has idle, run, attack, hurt and death, picking what plays next takes a lot of code. An AnimationTree plays the AnimationPlayer's animations through a state machine drawn in the editor. Point its Anim Player at the AnimationPlayer, set Tree Root to a new AnimationNodeStateMachine and connect the states:

  • Start to idle with Advance Mode Auto, so the tree starts in idle.
  • idle to attack, left at Enabled, so travel("attack") can take it.
  • attack to idle with Switch Mode At End and Advance Mode Auto, so the attack plays to its last frame and goes back by itself.

The script keeps extends, sprite, hitbox and face() from above, and the rest becomes three lines:

  • @onready var tree: AnimationTree = $AnimationTree
  • @onready var playback: AnimationNodeStateMachinePlayback = tree.get("parameters/playback")
  • func attack() -> void: playback.travel("attack")

The attack played, the tree went back to idle on its own, and the hitbox keys still gave 5 ticks and one hit. With the tree active, the AnimationPlayer's own play("attack") changed nothing on screen: the sprite kept the tree's idle frames.

Isometric characters: 8 directions with a BlendSpace2D

The isometric knight has one sheet per animation, each with 8 rows: s, sw, w, nw, n, ne, e, se. Idle is 4 frames at 6 fps and walk 8 at 10 fps, so standing and walking alone are 16 animations. In the tree each state becomes a BlendSpace2D with Blend Mode Discrete and 8 points: s at (0, 1), e at (1, 0), se at (0.71, 0.71) and so on. The sheets differ in width, so each animation also keys the Sprite2D's texture and hframes, and one Sprite2D plays both.

  • extends CharacterBody2D
  • @onready var tree: AnimationTree = $AnimationTree
  • @onready var playback: AnimationNodeStateMachinePlayback = tree.get("parameters/playback")
  • func move(dir: Vector2) -> void: playback.travel("idle" if dir == Vector2.ZERO else "walk"); face(dir)
  • func face(dir: Vector2) -> void: if dir != Vector2.ZERO: tree.set("parameters/idle/blend_position", dir.normalized()); tree.set("parameters/walk/blend_position", dir.normalized())

We fed it the 8 keyboard directions and the pack's own ground directions, which run 2:1 on screen (se moves 1.2 px right and 0.6 px down per frame). Both picked the right row in all 8 cases, and after walking nw the knight stood facing nw.

The ten Isometric Characters on a village map: knight, ranger, mage, rogue, barbarian, villager, skeleton, goblin, orc and cultist
Isometric Characters on Gridwright Isometric Tiles (sold separately): 10 characters, every direction drawn.

Hitboxes are where 8 directions get expensive: the isometric knight's attack (7 frames at 14 fps, hitting on frame 4) has its own box per direction. With AnimationPlayer that is 8 boxes keyed by hand; a frame_changed handler that looks the box up by facing keeps them in one function.

Where the Godot Animation Scene Builder fits

The Godot Animation Scene Builder does the AnimatedSprite2D setup from a sheet and its JSON (ours, Aseprite or TexturePacker): a SpriteFrames with animations like walk_down and idle_up_left, a CharacterBody2D scene with the AnimatedSprite2D on its feet, and a typed movement script for 8, 4 or 2 facings with play_once() for attacks. Add the Hitbox lines from the first script, and switch to an AnimationPlayer when the character needs one. Import settings: importing pixel art sprite sheets into Godot 4.

Checklist

  • Only pictures: AnimatedSprite2D. Hitboxes, sounds or spawns on exact frames: AnimationPlayer.
  • AnimatedSprite2D: switch the hitbox in frame_changed, go back to idle in animation_finished.
  • AnimationPlayer: Discrete frame tracks, and the hitbox keyed off at 0 in every animation.
  • With an AnimationTree active, change animations only through the tree.
  • For 8 directions, one Discrete BlendSpace2D per state.

Every Side-View Characters character has 8 animations facing both ways, with the hit frame and hitbox (or the arrow's and fireball's spawn point) in its JSON. Isometric Characters gives every melee attack a hit box per direction, and the Isometric Hero Creator adds 11 combat animations in 8 directions.

Our packs can go in commercial games but not be resold as files; see can I use game assets in a commercial game. If you need a character drawn for your game, you can order a custom pack.

Found it useful? Share it

Get the art for your game

Our packs together in the $20 Megabundle, a custom pack drawn for your game, or the packs in this post from $9.99 each.

Best value

Gridwright Megabundle

Our packs and tools on itch.io in one bundle, for less than three of them one by one.

Get it for $20 (opens in a new tab)

Custom packs from $49

Not finding it? We draw it for your game, in 14 days or your money back.

Order a custom pack

Packs mentioned

Related guides