Drawing Sprites
The Ultimate Guide to Gideros Studio
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: <syntaxhighlight lang="lua"> local childSprite = Sprite.new() </source>
- Add the new Sprite as a child: <syntaxhighlight lang="lua"> mySprite:addChild(childSprite) </source>
- Set the position: <syntaxhighlight lang="lua"> mySprite:setPosition(10, 20) </source>
- Set the scaling: <syntaxhighlight lang="lua"> mySprite:setScale(0.5, 1) </source>
- Set the rotation angle: <syntaxhighlight lang="lua"> mySprite:setRotation(90) </source>
- Set the alpha transparency: <syntaxhighlight lang="lua"> mySprite:setAlpha(0.7) </source>
- Get the first child: <syntaxhighlight lang="lua"> mySprite:getChildAt(1) </source>
- Remove the child: <syntaxhighlight lang="lua"> mySprite:removeChild(childSprite) </source>
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.