Created
February 7, 2025 18:47
-
-
Save luizbills/b91ca79d1d033322e8771546b30bd0df to your computer and use it in GitHub Desktop.
Tutorial: Simple Sprite Frame Animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let anim = {} | |
litecanvas() | |
function init() { | |
// each frame of the animation | |
// for this example, lets create 3 circles | |
anim.frames = [ | |
createFrame(3), | |
createFrame(4), | |
createFrame(5) | |
] | |
// current frame index | |
anim.curFrame = 0 | |
// 0.5 seconds for each frame | |
anim.frameTime = 0.5 | |
// next frame timer | |
anim.curTime = anim.frameTime | |
} | |
// this function controls the game logic | |
function update(dt) { | |
anim.curTime -= dt | |
if (anim.curTime <= 0) { | |
anim.curFrame = (anim.curFrame + 1) % anim.frames.length; | |
anim.curTime = anim.frameTime | |
} | |
} | |
// this function render the game scene | |
function draw() { | |
cls(0) | |
// draw the current frame of the animation | |
image(0, 0, anim.frames[anim.curFrame]) | |
} | |
function createFrame(color) { | |
return paint(128,128,() => { | |
circfill(64,64,64,color) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo