1 Reply Latest reply on Sep 16, 2011 10:16 AM by Flex harUI

    Basic container question

    SiHoop Level 1

      When I create a component that extends Sprite, I use a UIComponent in my parent app to serve as a container to the Sprite.However, this seems to be very awkward.

       

      Example 1:

      package      {
           import flash.display.Sprite;
           public class ChartFrame extends Sprite{
               public function ChartFrame( ){
                   super();
                   this.graphics.lineStyle(1, 0xFFFFFF);
                   this.graphics.drawRect(100, 100, 100, 100);   
               }
           }
      }

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  creationComplete="init()" >
           <mx:Script>
               <![CDATA[       
                   private function init():void{
                       var frame:ChartFrame=new ChartFrame();
                       container.addChild(frame);
                   }
               ]]>
           </mx:Script>
               <mx:UIComponent id="container"  />           
      </mx:Application>

       

      However, if I make the component a UIComponent instead of a Sprite, I can add the component directly to the application.

       

      Example 2:

      package      {
          import mx.core.UIComponent;
          public class ChartFrame extends UIComponent{
              public function ChartFrame( ){
                  super();
                  this.graphics.lineStyle(1, 0xFFFFFF);
                  this.graphics.drawRect(100, 100, 100, 100);   
              }
          }
      }

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  creationComplete="init()" >
          <mx:Script>
              <![CDATA[       
                  private function init():void{
                      var frame:ChartFrame=new ChartFrame();
                      this.addChild(frame);
                  }
              ]]>
          </mx:Script>          
      </mx:Application>

       

      My question: I don't understand why I cannot simply add the Sprite using 'this' instead of having to use a UIComponent. After all, isn't a UIContainer a subclass of a Sprite? Is it better to extend as a UIContainer than a Sprite?

      Thanks for your explanation!!

       

      Example 3 (fails):

      package      {
          import flash.display.Sprite;
          public class ChartFrame extends Sprite{
              public function ChartFrame( ){
                  super();
                  this.graphics.lineStyle(1, 0xFFFFFF);
                  this.graphics.drawRect(100, 100, 100, 100);   
              }
          }
      }

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  creationComplete="init()" >
          <mx:Script>
              <![CDATA[       
                  private function init():void{
                      var frame:ChartFrame=new ChartFrame();
                      this.addChild(frame);
                  }
              ]]>
          </mx:Script>           
      </mx:Application>