Manual Ship

On this page
  1. Export
  2. Draw it
  3. Pitfalls
  4. Next

LÖVE

Export a sprite as a plain Lua table of frames, animations and its pivot, and draw it in LÖVE with a few quads.

The love2d format writes one Lua file next to the atlas PNG. It returns a table with every frame's rectangle, every animation with its speed and loop setting, and the pivot at the feet. The file is plain Lua 5.1 or newer, so every LÖVE version reads it; the drawing code on this page is for LÖVE 11.

The starter hero walking in a LÖVE window at 2x
hero.lua drawn by LÖVE 11

Export#

Make a starter project to follow along, then export its hero:

bash
mode8 init my-game --template platformer
cd my-game
terminal
$ mode8 export assets/hero/frames --out-dir game/assets --name hero --formats love2d --animations assets/hero/master.m8.json
✓ Exported hero (15 frames) → game/assets/
  love2d · 4 files
  animations attack, hurt, idle, jump, walk
  palette nes_master_gothic from mode8.toml

Next:
  mode8 verify --assets-dir game/assets
$ cat game/assets/hero.lua
-- Generated by mode8: LOVE2D sprite atlas description.
return {
  image = "hero_atlas.png",
  width = 261,
  height = 293,
  fps = 8,
  pivot = { x = 36, y = 72 },
  order = {
    "windup",
    "attack",
    "recover",
    "hurt",
Show 37 more lines
    "idle",
    "idle_1",
    "idle_2",
    "idle_3",
    "jump",
    "walk_0",
    "walk_1",
    "walk_2",
    "walk_3",
    "walk_4",
    "walk_5",
  },
  frames = {
    ["windup"] = { x = 1, y = 1, w = 64, h = 72 },
    ["attack"] = { x = 66, y = 1, w = 64, h = 72 },
    ["recover"] = { x = 131, y = 1, w = 64, h = 72 },
    ["hurt"] = { x = 196, y = 1, w = 64, h = 72 },
    ["idle"] = { x = 1, y = 74, w = 64, h = 72 },
    ["idle_1"] = { x = 66, y = 74, w = 64, h = 72 },
    ["idle_2"] = { x = 131, y = 74, w = 64, h = 72 },
    ["idle_3"] = { x = 196, y = 74, w = 64, h = 72 },
    ["jump"] = { x = 1, y = 147, w = 64, h = 72 },
    ["walk_0"] = { x = 66, y = 147, w = 64, h = 72 },
    ["walk_1"] = { x = 131, y = 147, w = 64, h = 72 },
    ["walk_2"] = { x = 196, y = 147, w = 64, h = 72 },
    ["walk_3"] = { x = 1, y = 220, w = 64, h = 72 },
    ["walk_4"] = { x = 66, y = 220, w = 64, h = 72 },
    ["walk_5"] = { x = 131, y = 220, w = 64, h = 72 },
  },
  animations = {
    ["attack"] = { frames = { "windup", "attack", "recover" }, fps = 8, loop = false, durations = { 2, 1, 2 } },
    ["hurt"] = { frames = { "hurt" }, fps = 8, loop = false },
    ["idle"] = { frames = { "idle", "idle_1", "idle_2", "idle_3" }, fps = 4, loop = true, durations = { 2, 1, 2, 1 } },
    ["jump"] = { frames = { "jump" }, fps = 8, loop = false },
    ["walk"] = { frames = { "walk_0", "walk_1", "walk_2", "walk_3", "walk_4", "walk_5" }, fps = 10, loop = true },
  },
}

image is the atlas file, next to the Lua file. frames maps each frame name to its rectangle in the atlas, order lists them in atlas order, and animations names the frames of each animation with its fps and loop. pivot is the point between the feet, in frame pixels, the same for every frame.

Draw it#

A main.lua that plays the walk at 2x, standing on its feet at (240, 220):

lua
local sheet = love.filesystem.load("assets/hero.lua")()
local image, quads, anim, frame, clock

function love.load()
  love.graphics.setDefaultFilter("nearest", "nearest")   -- before loading any image
  image = love.graphics.newImage("assets/" .. sheet.image)
  quads = {}
  for name, f in pairs(sheet.frames) do
    quads[name] = love.graphics.newQuad(f.x, f.y, f.w, f.h, sheet.width, sheet.height)
  end
  anim, frame, clock = sheet.animations.walk, 1, 0
end

function love.update(dt)
  clock = clock + dt
  while clock >= 1 / anim.fps do
    clock = clock - 1 / anim.fps
    if anim.loop then
      frame = frame % #anim.frames + 1
    else
      frame = math.min(frame + 1, #anim.frames)
    end
  end
end

function love.draw()
  local q = quads[anim.frames[frame]]
  love.graphics.draw(image, q, 240, 220, 0, 2, 2, sheet.pivot.x, sheet.pivot.y)
end

The last two arguments of love.graphics.draw are the origin: drawing at the pivot puts the feet exactly on (240, 220) in every frame. To mirror the sprite, pass -2 as the x scale; the pivot keeps it on the same spot.

Pitfalls#

  • Blurry sprites. Call love.graphics.setDefaultFilter("nearest", "nearest") before newImage, or image:setFilter("nearest", "nearest") after it, and scale by whole numbers.
  • "Could not open file". sheet.image is relative to the Lua file. Prefix it with the folder you put both files in, as above.
  • The animation plays at the wrong speed. Use each animation's own fps; the top-level fps is only the export's default.

Next#