Figment wrote:
It's not hard to make on from a sprite sheet, just a little math and trial and error really.
Got any videos/text tutorials on this so I can get a closer look at the process?
To see if it looks familiar to any of my other work environments.
This'll only work for my class system, unless somone made a similar one, but here's my implementation. The first code block is for spritesheets, the second you just pass in an array of sprites. It's a bit rough and could be improved;
Code: Select all
Class "spritesheetAnimation" {
image,
cellWidth,
cellHeight,
["current"] = 1,
["framesPerUpdate"] = 5,
["maxFramesPerUpdate"] = 5,
["quads"] = {},
["parseSheet"] = function(s,Img,cw,ch)
s.cellWidth = cw
s.cellHeight = ch
s.image = Img
local amX = Img:getWidth()/cw
local amY = Img:getHeight()/ch
for y = 0, amY*ch-ch, ch do
for x = 0, amX*cw-cw, cw do
table.insert(s.quads, love.graphics.newQuad(x,y,cw,ch,Img:getWidth(),Img:getHeight()))
end
end
end,
["setFrames"] = function(s,fr)
s.framesPerUpdate = fr
s.maxFramesPerUpdate = fr
end,
["draw"] = function(s,x,y,w,h)
local lq = s.quads[s.current]
local w, h = w/s.cellWidth, h/s.cellHeight
love.graphics.drawq(s.image,lq,x,y,0,w,h)
love.graphics.print(s.current, 50,150,0,1,1)
end,
["update"] = function(s)
s.framesPerUpdate = s.framesPerUpdate - 1
if s.framesPerUpdate < 0 then
s.framesPerUpdate = s.maxFramesPerUpdate
s.current = s.current + 1
if s.current > #s.quads then
s.current = 1
end
end
end,
}
Code: Select all
Class "animation" {
["imageList"] = {},
["add"] = function(s,Img)
table.insert(s.imageList,Img)
end,
["current"] = 1,
["framesPerUpdate"] = 5,
["maxFramesPerUpdate"] = 5,
["setFrames"] = function(s,fr)
s.framesPerUpdate = fr
s.maxFramesPerUpdate = fr
end,
["draw"] = function(s,x,y,w,h)
local im = s.imageList[current]
if h then
local w = w/im:getWidth() --Fix scale so we can draw via pixels
local h = h/im:getHeight()
else
local h,w = im:getHeight(), im:getWidth()
end
love.graphics.draw(im,x,y,0,w,h)
end,
["update"] = function(s)
s.framesPerUpdate = s.framesPerUpdate - 1
if s.framesPerUpdate > 0 then
s.framesPerUpdate = s.maxFramesPerUpdate
s.current = s.current + 1
if s.current > #s.imageList then
s.current = 1
end
end
end,
}