Skip to main content
March 21, 2013
Question

How do i addChild from within an .AS class?

  • March 21, 2013
  • 1 reply
  • 437 views

i have a class that contains multiple Shapes... i want to apply addChild to all of these at once from within the class... something like:


public class MultiShape
{

      private var shapes:Vector.<Shape> = new Vector.<Shape>(3) ;

      public function MultiShape()

      {

            var i:int ;

            for ( i = 0 ; i < 3 ; i++)

            {

                      shapes = new Shape() ;
                      // setup shapes appaearance

                     addChild( shapes) ;   // *** doesn't work ***

            }

     }

}

i create instances of the class in the timeline code... there i can use 'addChild' by itself to add disply objects to the stage, as if the stage is 'this'... i would like to use/refer the stage in the class to be able to use 'addChild' to add the graphic Shape elements to the stage... but this i don't think is allowed...

a possible work around i considered was to create a Shape in the timeline, add it to the stage, wait for it to be added, and pass this Shape to the class so it could use that Shape's 'stage' property... but that seems a bit convoluted...

i should point out my class does not extend anything... it is meant to be a collection of graphic elements treated as one object...

This topic has been closed for replies.

1 reply

March 21, 2013

ok... i figured it out... a 'Sprite' is really also a DisplayObjectContainer... so if i make my class an extension of Sprite i can 'addChild' to the Sprite and then add them all to the stage at once by adding Sprite to the stage...