addChild()

Add display object as a child to this object.

Syntax addChild(object [, returnIndex])

Return Type displayObject or Number or Boolean

Description

Add display object as a child to this object. This means the child's x and y properties are now relative to the position of this display object. When this object is rotated, moved or removed, the children are also affected.

Arguments

object : displayObject
An object that inherits from the base displayObject. This could be either a predefined display object, or a user-defined created with the register() method.
returnIndex : Boolean
If set to true, the method will return the index of the child. See the return value below.

Return Value

displayObject or Number or Boolean. Returns the display object itself if the second argument is not specified or if it's false. If the second argument is specified, and is true, it returns the index of the child within the children object on the display object. Returns false if the passed in object already has a parent.

Examples

Example 1

We create a new core instance, a rectangle and a square. Then we add the rectangle to the canvas and finally add the square as a child to the rectangle.

View Example
Code
var canvas = oCanvas.create({
	canvas: "#canvas",
	background: "#ccc"
});

var rectangle = canvas.display.rectangle({
	x: 77,
	y: 270,
	width: 200,
	height: 100,
	fill: "#000"
});

var square = canvas.display.rectangle({
	x: 10,
	y: 10,
	width: 50,
	height: 50,
	fill: "#0ff"
});

canvas.addChild(rectangle);

rectangle.addChild(square);
Output