Animated sprites (SpriteFrames, AnimatedSprite2D)
Status: shipped (Gameplay toolkit G2a; ADR 0223,
editor ADR 0224). Tile maps (G2b), which reuse
SpriteSheetGrid for atlas sources, are in Tile maps.
Flipbook animation for the 2D canvas, Godot 4’s API: a SpriteFrames resource holds named animations
cut from sprite sheets, and an AnimatedSprite2D plays one with Godot’s timing and signals.
![]()
| Type | File | Role |
|---|---|---|
SpriteSheetGrid | MainframeEngine/Src/Scene/Nodes2D/Sprites/SpriteSheetGrid.cs | A uniform grid over an image: cell size, margins (top-left), separation |
SpriteFrame | Sprites/SpriteFrames.cs | [SerializableValue] record struct: texture index, region (texture pixels), relative duration |
SpriteAnimation | same | Inline resource: Name, Speed (fps, default 5), Loop (default on), Textures, Frames |
SpriteFrames | same | Resource (.mfres): Animations with cached, allocation-free name lookups |
AnimatedSprite2D | Sprites/AnimatedSprite2D.cs | The node: plays and draws the current frame |
var sheet = ResourceLoader.Load<Texture2D>("Assets/Sprites/hero.png");var frames = new SpriteFrames();var run = frames.AddAnimation("run");run.Speed = 10;run.AddFrames(sheet, new SpriteSheetGrid(new Vector2I(32, 32)), [new(0, 1), new(1, 1), new(2, 1), new(3, 1)]);ResourceSaver.Save(frames, "Assets/Sprites/hero.mfres");
var hero = new AnimatedSprite2D { SpriteFrames = frames, Autoplay = "run" };hero.AnimationFinished += OnLanded;Sprite sheets: SpriteSheetGrid
Section titled “Sprite sheets: SpriteSheetGrid”Godot’s “Select Frames” layout, also the layout of a tile set atlas (G2b): cells of CellSize start Margins pixels
from the top-left corner and are Separation apart.
FromCounts(imageSize, columns, rows, margins, separation): cell = (image − margins − separations) / count, integer division as in Godot’s dialog.GridSize(imageSize): whole cells that fit;CellRect(cell)/CellRegion(cell): texture pixels;CellAt(pixel): the cell under a point, (−1, −1) in a margin or a separation gap.
Slicing is data in the resource that uses it, not an import setting: a sheet stays one Texture2D with one .mfmeta,
and several animations (or a tile set) can cut it differently. Pixel-art sheets are drawn with the canvas item’s
TextureFilter, so set Nearest on the scene’s root Node2D (children inherit it).
SpriteFrames
Section titled “SpriteFrames”Each SpriteAnimation keeps a texture table and value-typed frames: SpriteFrame.Texture indexes Textures, a zero
Region draws the whole texture, Duration is relative (2 lasts twice as long as 1). There is no AtlasTexture and
no inline resource per frame. In a .mfres each frame is one line of six numbers [texture, x, y, w, h, duration]:
"SpriteAnimation_hdzjq": { "type": "SpriteAnimation", "props": { "Name": "run", "Speed": 10, "Textures": [ { "res": "Texture2D_srb15" } ], "Frames": [ [0, 0, 32, 32, 32, 1], [0, 32, 32, 32, 32, 1] ] }}API: GetAnimation(name), HasAnimation, GetFrameCount, AddAnimation, RemoveAnimation, RenameAnimation,
NotifyChanged() (after editing the list in place); on an animation AddFrame(texture, region, duration, at),
AddFrames(texture, grid, cells), TryGetFrame(index, out texture, out region), GetFrameDuration,
GetOrAddTexture. Name lookups go through a cached dictionary that is checked against the list on every hit, so
renames and in-place edits are seen at once and no lookup allocates.
AnimatedSprite2D
Section titled “AnimatedSprite2D”Exports (Godot’s names and defaults): SpriteFrames, Animation (“default”), Autoplay, Frame, FrameProgress,
SpeedScale, Centered, Offset, FlipH, FlipV. Methods: Play(name, customSpeed, fromEnd),
PlayBackwards(name), Pause(), Stop(), IsPlaying(), SetFrameAndProgress(frame, progress),
GetPlayingSpeed(), GetRect(), CurrentAnimation. Signals: AnimationChanged, AnimationFinished,
AnimationLooped, FrameChanged, SpriteFramesChanged.
- Timing is Godot’s
_notification(INTERNAL_PROCESS): a frame lastsDuration / (Speed × |playing speed|)seconds; the remainder carries over, so a long delta steps several frames and raisesFrameChangedonce per step. At the end a looping animation raisesAnimationLoopedthenFrameChanged; a non-looping one pauses on its last frame (the first when backwards) and raisesAnimationFinished. As in Godot, a step’s speed is taken before it moves to the next frame (uneven durations with long deltas show it). - Playing the current animation again resumes it, restarting only after it finished. A negative
SpeedScaleor custom speed plays backwards; settingFramethen starts at the end of that frame. - Drawing is
Sprite2D’s (sharedSpriteRects.Destination): oneTextureRectRegionquad of the frame’s region, centred or atOffset, flips as negative sizes. The node redraws only when the frame changes or the resource (or the current animation) raisesChanged. - No allocation per frame: name lookups, float maths, parameterless
Actionsignals and one quad per redraw. The unit gate plays 1 000 sprites for 300 ticks, the render-host gate 1 000 sprites for 240 frames, both at 0 bytes. - Edit mode: not a tool node, so it does not process in the editor and
Autoplayruns only in a game. The SpriteFrames editor previews by settingFrame(below).
With keyframe animation
Section titled “With keyframe animation”AnimatedSprite2D is a self-timed flipbook (walk cycles, idle loops, effects); an AnimationPlayer
(Animation) choreographs properties over time. They combine as in Godot: a player can key
Animation, Frame and FlipH or call Play/Stop from a method track once G1a has property and method tracks;
Sprite2D + Hframes/Vframes + a keyed Frame is the other Godot pattern. A frame strip is not a timeline track.
Editor
Section titled “Editor”- SpriteFrames editor (a custom inspector header on
SpriteFramesandAnimatedSprite2D): the animation list (name, frame count, fps, loop and autoplay marks) with Add, Duplicate, Rename, Delete, Loop, Autoplay on Load and a Speed field; the frame strip (thumbnails of each region, index, duration) with Play Backwards / Play / Stop (on a sprite), Add Frame from Image, Add Frames from Sprite Sheet, Copy, Paste, Move Left/Right, Delete and a Duration field. Icon buttons with tooltips. On a sprite, picking an animation sets itsAnimation; a sprite withoutSpriteFramesgets a New button. Every edit is one undo entry; a.mfresedited from a scene is saved with it. - Preview: Play steps the sprite’s
Framefrom the editor’s tick with no undo entry and without dirtying the scene; Stop (or the end of a non-looping animation) restores the frame. - Select Frames (
sheet_slicer.rml): the sheet with its grid; split by count (Horizontal × Vertical) or by cell size, with Separation and Offset; click cells to pick them (numbered in click order, the frame order); Select All, Clear, “Add N Frames”. The last grid per sheet is remembered in<project>/.mainframe/cache/sheet_grids.json. - New SpriteFrames from Sheet… (FileSystem context menu on an image): Select Frames, then
<image>.mfreswith one animation per picked row (“default” for a single row), opened in the inspector. - Picking: in a 2D tab a click inside a
Sprite2D’s orAnimatedSprite2D’s drawn rect selects it (after collision shapes and node origins).

- Unit (
Tests/MainframeEngine.Tests/Canvas/):SpriteSheetGridTests,AnimatedSprite2DTests(timing, durations, speed scale, loop and finish signal order, backwards, from the end, resume,SetFrameAndProgress, resource switching, autoplay in edit mode, drawing, redraws only on frame change, the 1 000-sprite allocation gate),SpriteFramesSerializationTests(.mfresround trip with one line per frame and identical bytes on re-save, the scene round trip, the codec, cached lookups). - Render (
CanvasRenderTests):animated-spriteat frames 1 and 15 (frames, both flips, offset, speed ×3, backwards, uneven durations, a stopped frame) withmoltenvkgoldens, and the 1 000-sprite allocation run. - Editor (
SpriteFramesEditorTests): the slicer model and grid memory, snapshot undo of every edit, the inspector actions on a sprite, the preview, Select Frames, “New SpriteFrames from Sheet”, sprite picking. - Demo:
sprites_2d(Sprites2DSceneTests, the scene file contract, the headless run).
Changes from the proposal
Section titled “Changes from the proposal”- The SpriteFrames editor is an inspector header, not a tab of a new bottom panel (ADR 0224); the bottom-panel tabs come with G2b’s TileMap palette.
SpriteAnimationkeeps its ownNameexport (notResourceName), as proposed; settingAnimationwithoutSpriteFrames, or assigning an emptySpriteFrames, keeps the names instead of clearing them (scene load order).- Not built: drag-reordering frames and frame zoom in the strip, auto-slice by opaque bounds, a project-wide default canvas texture filter.
- The Demo’s animated sprite has its own scene,
sprites_2d(Sprites 2D), with the generatedAssets/Sprites2D/hero.pngandhero.mfres, which the plannedtiles_2dscene can reuse; it is in the nav bar’s 2D category.
Related
Section titled “Related”2D canvas · Editor · Scene serialization · Demo · Tile maps