Skip to content

🖼️ SPRITES#

The sprite module allows you to load spritesheets and helps with animation.

Note

You can load 2 types of spritsheets:
1. If you have grid/strip like image, use col/rows to specify the number of columns and rows in the spritesheet.
2. Generate a spritesheet with Texture Packer using the ANTIRUINS exporter located in tools/texturepacker. Read More

Sprite Structure#

A sprite is represented by a table with the following structure:

local SPRITE = {
  frames    = {},  -- table of frames
  uvs       = {},  -- table of UV coordinates
  cFrame    = 1,   -- current frame
  maxFrame  = 1,   -- maximum number of frames
  direction = 1,   -- animation direction
  axis      = {x=1, y=1},  -- axis for scaling
  texture   = {},  -- texture object
  timer     = {},  -- timer handle
  stopAnimation = false,  -- flag to stop animation
}

Functions#

function sprite.new(spritefile, texture, col, row)
Creates and returns a new sprite object.

spritefile is the path to the sprite data file. If you're using col/rows, set this to nil. texture is the texture object or path to the texture file.
OPTIONAL:col and row are specifiy the number of columns and rows in the spritesheet.


function sprite:getCopy()
Returns a copy of the sprite object.


function sprite:draw(x, y, frame, sX, sY)
Draws the sprite at the specified x-y position.
frame is the frame number to draw.
OPTIONAL: sX and sY are scaling factors.


function sprite:nextFrame()
Advances to the next frame in the animation.
Returns the current frame number.


function sprite:loop(speed, startFrom, endFrame)
Loops the animation from startFrom to endFrame at the specified speed.


function sprite:play(speed, startFrom, endFrame)
Plays the animation from startFrom to endFrame at the specified speed.


function sprite:stop()
Stops the current animation.


function sprite:setFrame(frameNum)
Sets the frame number.


function sprite:getFrame()
Returns the current frame number.


function sprite:free()
Frees the texture data associated with the sprite.


function sprite:getSize()
Returns the width and height of the current sprite frame.