Skip to content

patrixr/love-animation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Love2D Animation library

A utility class to create animated sprites with Love2D.

Every animation is defined by:

  • an image file
  • an animation file (.lua)

The animation file describes the different frames and states of the animation, as well as the animation properties.

Take a look at animation.template.lua for an example.

Quick start

Loading the animation

local anim = LoveAnimation.new('sprites/animation1.lua');

Updating the animation

function love.update(dt)

  -- update things ...

	anim:update(dt)
end

Drawing the animation

function love.draw()

  -- draw things ...

	anim:draw()

end

Changing the animation state

anim:setState("jump")

Features

Changing state

Each state represent a row of the spritesheet, and has been defined in the animation file

anim:setState('jump')

Each state contains a nextState property which will run when all the frames have been iterated through. To create a looping animation, a state can simply have itself as the nextState

Reading the current state can be done with

anim:getCurrentState()

Speeding up / Slowing down

A "speed multiplier" can be set to either speed up or slow down the animation

anim:setSpeedMultiplier(0.5) -- slow down
anim:setSpeedMultiplier(2) -- speed up

Pausing the animation

anim:pause()
anim:unnpause()
anim:togglePause()

Callbacks

function doSomething()

end

anim:onStateStart('jumpStart', doSomething)

Cloning the animation

To create multiple instances of an animation, a clone method exists

anim:clone()

Scaling

The draw method accepts scaling arguments in order to change the size of the image

anim:draw(scaleX, scaleY)

Position / Geometry / Intersections

anim:getGeometry()
anim:intersects(x,y,width,height)
anim:setPosition(x, y)
anim:setRotation(r)
anim:setRelativeOrigin(ox, oy)
anim:setHorizontalFlip(true);
anim:getFrameWidth();
anim:getFrameHeight();
anim:getFrameDimension();

Visibility

anim:setVisibility(false)

Jumping to specific frames

The current frame can be manually selected

anim:setCurrentFrame(3)