ImageSheet

[gd] To make sprite animation, the getsheet() function and Sprite class are used. First, using the ImageSheet class, the frame information must be loaded.


[bl]  Assume that the image file 'catrun.png' is as follows



The whole width and height of this image is 2048x512. This image contains 8 frames and the size of each frame is 512x256.  The frame in the left-top corner has number 1. The frame number increases to the right and downwards.


  The arguments of the constructor of ImageSheet are the following four parameters and are all required.


fileURL, frame_width, frame_height, number_of_frames


local sht = ImageSheet('cat.png', 512, 256, 8)


  Next, a sequence information is to be made.

Single Sequence


  The sequence table contains the following information.


• time :  (required) play time in milli-second

• frames: (optional) table containing frame numbers to play.

• loops : (optional) number of repeatitions

              if loops are omitted, the animation plays repeatedly forever.


If the 'frames' table is omitted, the default table automatically generated contains all the frames in the image sheet.


  Some examples are as follows. Note that the 'time' field is always required.


local seq1 = {time = 1000} -- play all frames in 1 second once and repeat forever

local seq2 = {time = 500, frames={1,3,5,7} } -- play 1,3,5, and 7 frames in 0.5 second and repeat forever

local seq3 = {time = 1500, loops=1 } -- play once and stop

local seq4 = {time = 2000, frames={8,7,6,5,4,3,2,1}, loops=5 } -- play 5 times and stop


The two sheet and sequence informat are used in Sprite class


local s1 = Sprite(sht, seq1)


The animation is initially paused to play. If one want to create Sprite object and play at the same time, do as follows:


local s1 = Sprite(sht, seq1):play()


Then, the animation object is created and played immediately.

Multiple Sequences


The sequence table can have more than two sequence information. In this case, each sequence forms a single table. For example,


local seq = { 

        {time = 1000, loops=INF }, -- id : 1

        {time = 1500, frames={1,3,5,7}, loops=INF},  -- id:2

        {time = 2000, frames={8,7,6,5,4,3,2,1}, loops=5 } -- id:3

}


Each table is given an id in order and this id is used in play() method.


local s1 = Sprite(sht, seq1):play(1)  -- play 1st sequence

local s2 = Sprite(sht, seq1):play(3)  -- play 3rd sequence


  The sequence can have a name and play() method can use this user-designated name instead of automatically given id number.


local sht = ImageSheet('cat.png', 512, 256, 8)


local seq = { 

        walk= { time = 2000 },

        run = { time = 500, frames = {1,3,5,7} }

}


local cat = Sprite(sht, seq):play('walk')  -- play 'walk' sequence

Timer(5000, function() cat:play('run') end)  -- play 'run' sequence after 5 sec.


In this example, the cat sprite plays 'walk' named sequence at the beginning, and plays 'run' sequence after 5 seconds.


댓글 없음:

댓글 쓰기