Difference between revisions of "Sprite:addChild"

From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
 
=== Description ===
 
=== Description ===
 
Adds a sprite as a child to this sprite. The child is added as a last child of this '''Sprite''' instance.
 
Adds a sprite as a child to this sprite. The child is added as a last child of this '''Sprite''' instance.
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
Sprite:addChild(child)
 
Sprite:addChild(child)
</source>
+
</syntaxhighlight>
  
 
Sprites can only have 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 only have 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.
Line 15: Line 15:
  
 
=== Example ===
 
=== Example ===
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
local sprite1 = Sprite.new()
 
local sprite1 = Sprite.new()
 
local sprite2 = Sprite.new()
 
local sprite2 = Sprite.new()
Line 28: Line 28:
 
stage:addChild(sprite1)
 
stage:addChild(sprite1)
 
stage:addChild(sprite2)
 
stage:addChild(sprite2)
</source>
+
</syntaxhighlight>
  
 
{{Sprite}}
 
{{Sprite}}

Latest revision as of 14:33, 13 July 2023

Available since: Gideros 2011.6
Class: Sprite

Description

Adds a sprite as a child to this sprite. The child is added as a last child of this Sprite instance.

Sprite:addChild(child)

Sprites can only have 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.

Parameters

child: (Sprite) the child sprite to add

Example

local sprite1 = Sprite.new()
local sprite2 = Sprite.new()
sprite1:setPosition(1 * 64, 64)
sprite2:setPosition(3 * 64, 64)

local mypixel = Pixel.new(0xFF0000, 1, 32, 32)

sprite1:addChild(mypixel)
sprite2:addChild(mypixel)

stage:addChild(sprite1)
stage:addChild(sprite2)