I just started learning LOVE many days ago.
I'm wondering how to make short animations. I mean, like breaking a barrel, a bomb exploding etc. However, I don't have any actual LOVE examples to refer to.
And this is my idea:
1. load each frame of the animation into LOVE
2. draw the images with a time interval
this is my example code:
Code: Select all
function love.load() --
ww = love.graphics.getWidth()
wh = love.graphics.getHeight() --window width and height
love.graphics.setBackgroundColor(255,255,255,255)
redtangle:init() --load images
end
function love.draw()
redtangle:draw()
end
function love.update(dt) --
redtangle:play()
end
redtangle = {
frames = {},
playFrame = 1,
playCount = 0,
playInterval = 10, --each frame lasts 10 * dt
}
function redtangle:init()
local tName
for i = 1, 4 do
tName = "p"..tostring(i)..".png" --load images, p1.png to p4.png
self.frames[i] = love.graphics.newImage(tName)
end
end
function redtangle:play()
self.playCount = self.playCount + 1
if self.playCount > self.playInterval then
self.playCount = 0
self.playFrame = self.playFrame + 1 --switch to next frame
if self.playFrame > 4 then
self.playFrame = 1
end
end
end
function redtangle:draw()
local rx, ry = ww/2 - 125, wh/2 - 125
love.graphics.draw(self.frames[self.playFrame], rx, ry) --draw
end