Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(+1)

I create a code snippet for animations :) Hope its useful for someone https://tic.computer/play?cart=12

-- title:  Animation Testing
-- author: Ahmed Khalifa
-- desc:   Function to draw sprite animation
-- script: lua
-- input:  gamepad


-- create animation object
-- frames: array of the upper left corner of each frame
-- size: 0 means 8x8, 1 means 16x16, etc
-- fps: frames per seconds
function createAnimation(frames,size,fps)
    return {cf=0,frames=frames,size=size,t=1/fps*1000,spf=1/fps*1000}
end


-- reset the animation to the original frame
-- anim: animation object to reset
function resetAnimation(anim)
    anim.cf=0 anim.t=anim.spf
end


-- Draw the current animation object
-- anim: animation object to draw
-- dt: delta time since last update
-- x,y: position of upper left corner
-- ts: transparent color
-- flip: 0 no flip 1 flip horizontally
function drawAnimation(anim,dt,x,y,ts,flip)
    anim.t=anim.t-dt
    if anim.t<=0 then 
        anim.t=anim.t+anim.spf
        anim.cf=anim.cf+1
        if anim.cf>=#anim.frames then
            anim.cf=0
        end
    end
    local f=anim.frames[anim.cf+1]
    for i=0,anim.size do
        for j=0,anim.size do
            if flip==0 then
                spr(f+i+j*16,x+i*8,y+j*8,ts,1,flip)
            else
             spr(f+i+j*16,x+(anim.size-i)*8,y+j*8,ts,1,flip)
            end
        end
    end
end


-- initialize the required data
function init()
    pt=time()
    dt=0
    x=84
    y=64
    flip=0
    -- 16x16 animation
    big=createAnimation({256,258,260,258,256,262,264,262},1,20)
    -- 8x8 animation
    small=createAnimation({266,267,268,267,266,269,270,269},0,20)
    -- the current activated animation
    anim=big
end
init()


-- update function
function TIC()
    -- calculate delta time
    dt=time()-pt
    pt=time()
    
    -- change the animation state and direction based on controls
    state=0
    if btn(2) then state=1 flip=1 x=x-1 end
    if btn(3) then state=1 flip=0 x=x+1 end
    if btnp(4,60,6) then
        if anim==big then
            anim=small
        else
            anim=big
        end
    end
    
    -- clear screen
    cls(12)
    -- reset the current animation when no btns pressed
    if state==0 then resetAnimation(anim) end
    -- draw the screen
    drawAnimation(anim,dt,x,y,15,flip)
    print("Press Z to change Animation",50,100,0)
end