Drawing Sprites

From GiderosMobile
Revision as of 20:15, 10 September 2018 by Anthony (talk | contribs) (Created page with "Sprite is the base class of all display classes and has no visual representation. It’s the main construction element and used to create display hierarchies. Sprites can have...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Sprite is the base class of all display classes and has no visual representation. It’s the main construction element and used to create display hierarchies. Sprites can have other sprites as their children and these children inherit properties such as position, scaling and transparency from their parent sprite. Hierarchy is an important feature in graphics - when you move your parent sprite, all the child (and grandchild) sprites also move simultaneously.

Here are some examples:

- Create a new Sprite instance:

local childSprite = Sprite.new()

- Add the new Sprite as a child:

mySprite:addChild(childSprite)

- Set the position:

mySprite:setPosition(10, 20)

- Set the scaling:

mySprite:setScale(0.5, 1)

- Set the rotation angle:

mySprite:setRotation(90)

- Set the alpha transparency:

mySprite:setAlpha(0.7)

- Get the first child:

mySprite:getChildAt(1)

- Remove the child:

mySprite:removeChild(childSprite)

Sprites can have only one parent. Therefore if you add a child object that already has a different sprite as a parent, the sprite is removed from the child list of the other sprite and then added to this sprite.