10 Replies Latest reply on Sep 16, 2009 4:35 AM by SiHoop

    Using containers

    SiHoop Level 1

      I havea container question that has been bothering me for a long time: How am I supposed to add an instance of a class to the stage? In the file below I have 3 ways to add an instance of the class Shapes (listed below). Approach 1 uses a UIComponent named container. This works fine but feels wrong as I am having to add a component. Approach 2 uses rawChildren. This works fine but it feels like a 'work around'. Approach 3 (this.addChild(shape);) fails and gives me an error, although it 'feel right'.

       

      Is Approach 1 the right way to add instances to the stage or is there a better way?

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
          <mx:Script>
              <![CDATA[
      private var shape:Shapes
      public function init():void{
          shape=new Shapes();

          container.addChild(shape);
          //rawChildren.addChild(shape);

          //this.addChild(shape);
      }
              ]]>
          </mx:Script>
      <mx:UIComponent id="container" />
      </mx:Application>

       

      package {
          import flash.display.Shape;
          import flash.display.Sprite;
          public class Shapes extends Sprite {
              public function Shapes() {
                 var child:Shape = new Shape();
                  child.graphics.beginFill(0xFFCC00);
                  child.graphics.lineStyle(0);
                  child.graphics.drawRect(0, 0, 50, 50);
                  child.graphics.endFill();
                  addChild(child);
              }
          }
      }