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.
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")
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()
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
anim:pause()
anim:unnpause()
anim:togglePause()
function doSomething()
end
anim:onStateStart('jumpStart', doSomething)
To create multiple instances of an animation, a clone method exists
anim:clone()
The draw
method accepts scaling arguments in order to change the size of the image
anim:draw(scaleX, scaleY)
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();
anim:setVisibility(false)
The current frame can be manually selected
anim:setCurrentFrame(3)