Sprite.lua
sprite.lua is a library for creating high-level animated custom sprites with movement abilities. It requires LunaLua v0.7.1.
Installation
Copy the sprite.lua file in the LuaScriptsLib folder.
How to use and Examples
To enable the sprite library for a specific level, add this line to lunadll.lua:
local SpriteAPI = loadAPI("sprite");
This will load the Sprite API.
Creating an animated sprite from multiple images
local myImages = {
Graphics.loadImage(Misc.resolveFile("frame1.png")),
Graphics.loadImage(Misc.resolveFile("frame2.png")),
Graphics.loadImage(Misc.resolveFile("frame3.png"))
}
local myAnimationSet = SpriteAPI.animationFromImages(myImages)
local mySprite = SpriteAPI.sprite(myAnimationSet, SpriteAPI.COOR_CAMERA, 100, 100)
mySprite:show()
In this example the three files "frame1.png", "frame2.png", "frame3.png" are bundled into an animation set. This animation set is passed to the sprite constructor.
This is an easy way to create an animated sprite. But you will see that the animation goes insanely fast.
To fix that you can add this line:
mySprite:setFrameSpeed(8)
With this line only every 8 frames a frameswitch is happening.
Creating an animated sprite from an animated gif
The sprite api can also load animated gifs:
local gifAnimationSet, gifFrameSpeed = SpriteAPI.animationFromGif(Misc.resolveFile("Rex.gif"))
local gifSprite = SpriteAPI.sprite(gifAnimationSet, SpriteAPI.COOR_CAMERA, 200, 200)
gifSprite:setFrameSpeed(gifFrameSpeed)
gifSprite:show()
In this example we load the gif "Rex.gif" directly into an animation set. In addition we get the frame speed (written in the gif metadata) returned which we can use with the :setFrameSpeed-function. This is the simplest way to create an animated sprite, because you don't have to care about the frame speed.
You can create animated sprites (with built-in frame delay) with gimp and its gif exporter.