Sprite

Type    

class 

[gd]

Inheritance

Sprite ⟶ Display ⟶ Object

[bg]

Overview

Create sprite animation.


See also 

Display

Sprite animation tutorial

getsheet()



Overview


Creates an sprite animation the screen from ImageSheet object and sequence information. Initially, the animation is stopped.

The local origin is at the center of the image.

Initial anchor point is the center of the image also.


  Sprite class inherits Display class, therefore all the methods defined in Display class can also be used in the Sprite object.



Syntax



Sprite(sheet, sequence)


    • sheet  (required) : returned table of getsheet() function

    • sequence (required) : table containing sequence data

          • time (required) : milli-seconds, time to play all the frames once

          • loops  : (number) repeat count whose default value is INF

          • frames : (table) 

          (Note:) In case of multiple sequences, a number of tables containing above fields

                      can exist in the sequence table     


Methods



method

description

obj:setFrame(n)

Set displaying image as nth frame (If the animation was playing, stop it and then set the image.) 

obj:play()

obj:play(name_or_num)

play default (or single) sequence

play named (or numbered) sequence

obj:stop()

stop playing animation

obj:pause()

pause playing animation

obj:resume()

resume the stopped animation


Note that the constructor automatically sets the frame as displaying image.

The sequence is not played at the beginning.



Examples



single sequence :

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

local cat = Sprite(sht, {time = 1000} ):play() 

-- play all frames in 1 second and repeat forever



multiple sequences with number indexes :

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

local seq = { 

        {time = 1000 } -- id : 1

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

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

}

local cat = Sprite(sht, seq):play(1)  -- play 1st sequence 

cat:addTimer(3000, function(self) self:play(2) end) -- play 2nd sequence after 3 second

cat:addTimer(5000, function(self) self:play(3) end) -- play 3rd sequence after 5 second


multiple sequences with names :

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

local seq = { 

        walk={ time = 2000 },

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

}

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

cat:addTimer(5000, function(self) self:play(2) end)  -- play 'run' sequence after 5 sec


댓글 없음:

댓글 쓰기