How to Use Addchild in Action Script

ActionScript 3.0 uses a very powerful display list through which objects are displayed in flash. The display list can be applied only to objects that are visual, like shapes, text box and lines. Values and variable are non visual objects and cannot be used by display list. Display list uses two important functions; addChild() actionscript 3 and removeChild(). addChild() actionscript is used to display object to the screen and removeChild() to remove the object from the screen. Let’s look into a simple example to add a circle shape and a text box.


Step 1: Open a new flash project

First, let’s open a new flash project. ActionScript 3.0 should be the type of the file chosen.

Open Flash Project

Step 2: Create an object

Create an object myText of class TextField and instantiate the same. Add some text to myText. To make the objects visible in the stage, use the addChild() actionscript method as follows:
This text can be formatted, resized and repositioned. There are functions available for all the text manipulation.

Code – Text Field

Step 3: Creating a symbol

Previously in ActionScript 2.0, there were classes to create shapes instantly. This is removed in ActionScript 3.0. So to import the shapes, we should create them manually.
To start with, let’s create a circle and convert it to a symbol. In the ‘convert to symbol’ window, choose the option ‘Export for ActionScript’ in the advanced settings and type in the class name as ‘Circle’. This movie clip is now registered in the library. Delete of the circle from the stage area.

Create Symbol

Step 4: New Circle

Similar to the class TextField, we have a class for the circle. To instantiate the class, let’s type in the following code followed by the addChild() actionscript 3 function to display on to the stage:

Code – Circle

Step 5: Remove Circle

To view the circle and the text on the stage, test the movie by pressing CTRL+Enter. They are present one after the other. Extend the timeline till frame 60. Now we are going to add the removeChild() function. So at frame 60, the Circle should be removed from the screen. With frame 60 selected, open the action window and type in removeChild(myCircle).
We are also adding a stop(); function to the code, to stop the test movie from looping. Now let’s test the movie. Notice that the Circle Stays for 2 seconds and disappears. It is due to the removeChild() function.

Output

Conclusion
The position of the objects can be controlled by altering the x and y axis. Other object properties that can be changed are skew, size, font, font size and many more.