Love - Animation

From TRCCompSci - AQA Computer Science
Revision as of 09:50, 27 June 2019 by Admin (talk | contribs) (Load in the SpriteSheet)
Jump to: navigation, search

Requirements

You need to have followed the installation process for the Love engine.

You also need to have created a minimal game (ie a new folder, with a 'main.lua' file)

You need to have added this code to 'main.lua':

function love.load()

end

function love.update(dt)

end
 
function love.draw()

end

Required Variables

We need to add the following variables before your 'love.load()':

fps = 10
width = 120
current = 0
frames = 4
duration = 1/fps

The variable fps is the number of frames per second, this will give you some degree of control and also compensate for varying processing power.

Width is the width of a single frame, if your sprite sheet contained multiple rows (normally one per animation) you would also need to use height.

Current will be the current frame, and frames is the total number of frames in this animation.

Duration is calculated from the frames per second, this will be used to ensure we only switch frames after a certain amount of time has passed.

Load in the SpriteSheet

Now in the 'love.load()' you need to add the following:

spritesheet = love.graphics.newImage("player.png")
animation = love.graphics.newQuad(current*width, 0, 120, 120, spritesheet:getDimensions())

spritesheet will be the entire image, and animation is essentially a rectangle to represent the part of the spritesheet for the current frame. '120' is the width and height of each frame.

Switch Frames

Draw current frame