3 Replies Latest reply on Mar 3, 2009 7:01 AM by Newsgroup_User

    Problem adding to the display list from a class

    SiHoop Level 1
      If I try to convert as3 in an mxml file into a class file and create an instance of the class, I am unable to add the instance to the display list. The original mxml file is at the bottom of the page. The modified mxml file and its associated class are below. The problem is that I cannot use addChild(myComponent); So how to I add the UIComponent to the Display list?

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
      <mx:Script>
      <![CDATA[
      import LoadSwf
      private var myLoader:LoadSwf
      public function init():void{
      myLoader=new LoadSwf()
      }
      ]]>
      </mx:Script>
      </mx:Application>
      Class:
      package{
      import flash.display.MovieClip;
      import com.adobe.viewsource.ViewSource;
      import flash.events.Event
      import mx.core.UIComponent
      import flash.display.Loader
      import flash.events.TimerEvent;
      import flash.events.StatusEvent
      import flash.net.LocalConnection
      import flash.net.URLRequest
      import flash.net.URLVariables
      import flash.utils.Timer;
      public class LoadSwf{
      private var request:URLRequest
      private var loader:Loader = new Loader();
      private var conn:LocalConnection = new LocalConnection();
      private var myComponent:UIComponent=new UIComponent();
      private var swfTimer:Timer;
      private var myParameters:URLVariables ;
      public function LoadSwf(){
      request= new URLRequest("Login.swf");
      myParameters= new URLVariables()
      myParameters.loginID = "101";
      myComponent.addChild(loader);
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishedLoading);
      loader.load(request);
      }
      private function finishedLoading(e:Event):void{
      //PROBLEM IN THE FOLLOWING LINE!!!!!!!
      addChild(myComponent);
      swfTimer = new Timer(10);
      swfTimer.addEventListener(TimerEvent.TIMER,sendData);
      swfTimer.start();
      }
      private function sendData(e:Event):void{
      conn.addEventListener(StatusEvent.STATUS, onStatus);
      conn.send("myConnection", "lcHandler", myParameters.loginID);
      }
      private function onStatus(event:StatusEvent):void {
      if (event.level=="status") {
      trace("LocalConnection connected");
      swfTimer.stop();
      }else if (event.level=="error"){
      trace("FAILED to connect")
      }
      }

      }
      }
        • 1. Re: Problem adding to the display list from a class
          Level 7

          "SiHoop" <webforumsuser@macromedia.com> wrote in message
          news:gohnni$n9h$1@forums.macromedia.com...
          > If I try to convert as3 in an mxml file into a class file and create an
          > instance of the class, I am unable to add the instance to the display
          > list. The
          > original mxml file is at the bottom of the page. The modified mxml file
          > and its
          > associated class are below. The problem is that I cannot use
          > addChild(myComponent); So how to I add the UIComponent to the Display
          > list?
          >
          > <?xml version="1.0" encoding="utf-8"?>
          > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
          > layout="absolute"
          > creationComplete="init()">
          > <mx:Script>
          > <![CDATA[
          > import LoadSwf
          > private var myLoader:LoadSwf
          > public function init():void{
          > myLoader=new LoadSwf()
          > }
          > ]]>
          > </mx:Script>
          > </mx:Application>
          > Class:
          > package{
          > import flash.display.MovieClip;
          > import com.adobe.viewsource.ViewSource;
          > import flash.events.Event
          > import mx.core.UIComponent
          > import flash.display.Loader
          > import flash.events.TimerEvent;
          > import flash.events.StatusEvent
          > import flash.net.LocalConnection
          > import flash.net.URLRequest
          > import flash.net.URLVariables
          > import flash.utils.Timer;
          > public class LoadSwf{
          > private var request:URLRequest
          > private var loader:Loader = new Loader();
          > private var conn:LocalConnection = new LocalConnection();
          > private var myComponent:UIComponent=new UIComponent();
          > private var swfTimer:Timer;
          > private var myParameters:URLVariables ;
          > public function LoadSwf(){
          > request= new URLRequest("Login.swf");
          > myParameters= new URLVariables()
          > myParameters.loginID = "101";
          > myComponent.addChild(loader);
          > loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
          > finishedLoading);
          > loader.load(request);
          > }
          > private function finishedLoading(e:Event):void{
          > //PROBLEM IN THE FOLLOWING LINE!!!!!!!
          > addChild(myComponent);

          You didn't add this component to the Application, so I wouldn't expect to
          see anything you add in this component in the Application.

          HTH;

          Amy


          • 2. Problem adding to the display list from a class
            SiHoop Level 1
            I figured out that I can change the UIComponent to a Sprite or MovieClip in the class, and then add the instance to a UIComponent in the mxml file to attach it to the stage, although I can't for the life of me understand why. What I really want to know is how I would go about using the class in Flash. I added the following to the timeline, but I get an error message telling that mx.core:UIComponent cannot be found. Can you tell me how to add an instance of the class in Flash or should I go elsewhere with that question?

            import LoadSwf
            import mc.core.UIComponent
            var myLoader:LoadSwf
            myLoader=new LoadSwf()
            addChild(myLoader)
            • 3. Re: Problem adding to the display list from a class
              Level 7

              "SiHoop" <webforumsuser@macromedia.com> wrote in message
              news:goht9m$7j$1@forums.macromedia.com...
              > Good point! But even when I add and connect to a UIComponent, the instance
              > does
              > not show up.
              >
              > <?xml version="1.0" encoding="utf-8"?>
              > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
              > layout="absolute"
              > creationComplete="init()">
              > <mx:Script>
              > <![CDATA[
              > import LoadSwf
              > private var myLoader:LoadSwf
              > public function init():void{
              > myLoader=new LoadSwf()
              > mapContainer.addChild(myLoader)
              > }
              > ]]>
              > </mx:Script>
              > <mx:UIComponent id="mapContainer" />
              > </mx:Application>

              try:

              myLoader.setActualSize(myloader.measuredHeight, myLoader.measuredWidth);

              Make sure you do this only after the loader has loaded its content.

              HTH;

              Amy