Difference between revisions of "Drawing Sprites"
m (Text replacement - "<source" to "<syntaxhighlight") |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
Line 8: | Line 8: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
local childSprite = Sprite.new() | local childSprite = Sprite.new() | ||
− | </ | + | </syntaxhighlight> |
- Add the new Sprite as a child: | - Add the new Sprite as a child: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
mySprite:addChild(childSprite) | mySprite:addChild(childSprite) | ||
− | </ | + | </syntaxhighlight> |
- Set the position: | - Set the position: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
mySprite:setPosition(10, 20) | mySprite:setPosition(10, 20) | ||
− | </ | + | </syntaxhighlight> |
- Set the scaling: | - Set the scaling: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
mySprite:setScale(0.5, 1) | mySprite:setScale(0.5, 1) | ||
− | </ | + | </syntaxhighlight> |
- Set the rotation angle: | - Set the rotation angle: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
mySprite:setRotation(90) | mySprite:setRotation(90) | ||
− | </ | + | </syntaxhighlight> |
- Set the alpha transparency: | - Set the alpha transparency: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
mySprite:setAlpha(0.7) | mySprite:setAlpha(0.7) | ||
− | </ | + | </syntaxhighlight> |
- Get the first child: | - Get the first child: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
mySprite:getChildAt(1) | mySprite:getChildAt(1) | ||
− | </ | + | </syntaxhighlight> |
- Remove the child: | - Remove the child: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
mySprite:removeChild(childSprite) | mySprite:removeChild(childSprite) | ||
− | </ | + | </syntaxhighlight> |
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. | 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. |
Latest revision as of 14:27, 13 July 2023
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:
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.