gavin sim logo
 

Week 1

Week 2

Week 3

Week 4

Week 5

Week 6

Week 7

Week 8

Week 9

Teaching > ActionScript

addChild

Part 1

Adding a displayobject to the display requires two steps, the first create the object and the second to add it to the stage.

Create a new flash document and add the following code.

var mc:MovieClip = new MovieClip();
addChild(mc);

This will create a blank movieClip and add it to the stage.

Next we will add some text inside the MovieClip. To do this add the following code:

var myText:TextField = new TextField();
myText.text = "This is inside the movieclip!";
myText.width = 300;
mc.addChild(myText);

This code will create a new text field varaible, the text is then assigned to the variable and the width is increased from the default value to 300. The last line uses the blank MovieClip as a container and the text is added to this. The MovieClip is a parent to the child (myText). If you remove the parent from the stage you will also remove the child.

To reposition the text you could simply alter the X and Y co-ordinates of the movieclip or the text. To change the movie clips position you would add the following.

mc.x=50;
mc.y=75;

This code simply repositions the movie clip so that the x value is equal to 50 and the Y is equal to 75.

 

Excercise

Alter the code so that you add another textfield containing the words "Text Box 2" with this having a location of x=150 and Y=200. Also create a button so that when that is pressed the movieclip mc is removed from the stage hint removeChild();