Difference between revisions of "Drawing Sprites"

From GiderosMobile
m
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(One intermediate revision by the same user not shown)
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>
+
</syntaxhighlight>
  
 
- 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>
+
</syntaxhighlight>
  
 
- Set the position:
 
- Set the position:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:setPosition(10, 20)
 
mySprite:setPosition(10, 20)
</source>
+
</syntaxhighlight>
  
 
- Set the scaling:
 
- Set the scaling:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:setScale(0.5, 1)
 
mySprite:setScale(0.5, 1)
</source>
+
</syntaxhighlight>
  
 
- Set the rotation angle:
 
- Set the rotation angle:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:setRotation(90)
 
mySprite:setRotation(90)
</source>
+
</syntaxhighlight>
  
 
- Set the alpha transparency:
 
- Set the alpha transparency:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:setAlpha(0.7)
 
mySprite:setAlpha(0.7)
</source>
+
</syntaxhighlight>
  
 
- Get the first child:
 
- Get the first child:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:getChildAt(1)
 
mySprite:getChildAt(1)
</source>
+
</syntaxhighlight>
  
 
- Remove the child:
 
- Remove the child:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mySprite:removeChild(childSprite)
 
mySprite:removeChild(childSprite)
</source>
+
</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.