Skip to main content
markerline
Inspiring
December 20, 2010
Answered

addChild() vs addChildAt() methods?

  • December 20, 2010
  • 3 replies
  • 2800 views

I have the following code:

var container_mc:Container = new Container();

for (var i:int=0;i<4;i++){

var myTrap_mc:MyTrap = new MyTrap();

var myTrapR_mc:MyTrapR = new MyTrapR();

container_mc.addChildAt(myTrap_mc,i);

container_mc.addChildAt(myTrapR_mc,i);

myTrap_mc.rotate=i*90;

myTrapR_mc.rotate=i*90;

container_mc.x=20;

container_mc.y=20;

trace(myTrap_mc.rotate);

trace(container_mc.x+" x");

}

MyTrap and MyTrapR are sheared rectangles in my Library with Export at Frame 1 selected.  When I test the movie I don't see any objects on the stage.

If I simply declare addChild(myTrap_mc) within the loop it will add one child instead of 4 which is why I tried using a container and adding the children to the container.  Still no beans.

How can I get these sheared rectangles to appear so that they form an 8-pointed star?  This is simply an exercise in drawing shapes dynamically using ActionScript.  (Of course I'm not using ActionScript graphics properties so it's not truly dynamic in that sense).

This topic has been closed for replies.
Correct answer

You don't need a container. I think the problem is that you are using _mc.rotate instead of _mc.rotation. Use Debug/List Objects to see if all 8 objects are being placed. BTW, you only have to loop through twice to get an eight pointed star.

3 replies

Correct answer
December 20, 2010

You don't need a container. I think the problem is that you are using _mc.rotate instead of _mc.rotation. Use Debug/List Objects to see if all 8 objects are being placed. BTW, you only have to loop through twice to get an eight pointed star.

markerline
Inspiring
December 20, 2010

Many thanks to all 3 of you.

Yes, I forgot to addChild(container_mc) which is why I didn't get a display.  But even then I had container_mc.myTrap_mc.rotation=i*90; and this resulted in an output error (term is undefined . . . ).  With the following code everything works and looks like a Christmas Sweater star:

var container_mc:Container = new Container();

addChild(container_mc);

for (var i:int=0;i<4;i++){

var myTrap_mc:MyTrap = new MyTrap();

var myTrapR_mc:MyTrapR = new MyTrapR();

container_mc.addChild(myTrap_mc);

container_mc.addChild(myTrapR_mc);

myTrap_mc.rotation=i*90;

myTrapR_mc.rotation=i*90;

container_mc.x=200;

container_mc.y=200;

trace(myTrap_mc.rotation);

trace(container_mc.x+" x");

}

I do not understand why I can use myTrap_mc.rotation=i*90;

instead of

container_mc.myTrap_mc.rotation=i*90;

when I have the statement container_mc.addChild(myTrap_mc);

I will mark the question as answered if someone can explain to me the logic behind that in AS 3.0 .  To me it looks similar to an incorrect URL statement when you are working with directories and directory structure to address files in a path, if the files were addressed with dot-syntax.

December 20, 2010

You can say myTrap_mc because you declared it in the code: var myTrap_mc:MyTrap = new MyTrap(); so AS has a reference to that object regardless of what container you place it in.

You cannot reference it as container_mc.myTrap_mc since myTrap_mc is a reference to the object, not an instance name.

If you had put the object myTrap_mc inside of a container on the stage in Flash you would have had to address it as container_mc.myTrap_mc unless of course the code was inside of container_mc.

December 20, 2010

var container_mc:Container = new Container();

addChild(container_mc); // <---- add this line

Ned Murphy
Legend
December 20, 2010

I can see where you create a container, but I can't see where you add it to the display list  (addChild(container)