Difference between revisions of "Drawing Sprites"

From GiderosMobile
m
m (Text replacement - "<source" to "<syntaxhighlight")
Line 6: Line 6:
  
 
- Create a new Sprite instance:
 
- Create a new Sprite instance:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
local childSprite = Sprite.new()
 
local childSprite = Sprite.new()
 
</source>
 
</source>
  
 
- Add the new Sprite as a child:
 
- Add the new Sprite as a child:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:addChild(childSprite)
 
mySprite:addChild(childSprite)
 
</source>
 
</source>
  
 
- Set the position:
 
- Set the position:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:setPosition(10, 20)
 
mySprite:setPosition(10, 20)
 
</source>
 
</source>
  
 
- Set the scaling:
 
- Set the scaling:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:setScale(0.5, 1)
 
mySprite:setScale(0.5, 1)
 
</source>
 
</source>
  
 
- Set the rotation angle:
 
- Set the rotation angle:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:setRotation(90)
 
mySprite:setRotation(90)
 
</source>
 
</source>
  
 
- Set the alpha transparency:
 
- Set the alpha transparency:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:setAlpha(0.7)
 
mySprite:setAlpha(0.7)
 
</source>
 
</source>
  
 
- Get the first child:
 
- Get the first child:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:getChildAt(1)
 
mySprite:getChildAt(1)
 
</source>
 
</source>
  
 
- Remove the child:
 
- Remove the child:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:removeChild(childSprite)
 
mySprite:removeChild(childSprite)
 
</source>
 
</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.
 
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.

Revision as of 14:26, 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: <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.