Sprite.lua

From Moondust Wiki
Revision as of 11:49, 20 August 2015 by Kevsoft (talk | contribs)
Jump to navigation Jump to search

Download Latest Version

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

First of all you want load your images in a table:

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 the easiest way to get 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.