6 Replies Latest reply on Feb 8, 2010 11:29 AM by tobi3

    How to load and instantiate FXG at runtime?

    tobi3

      Hi

       

      Let's say I have a Flex 4 app whose SWF size in KB/MB should be small. While the app runs, the user might want to make the app load 10 of 1000 FXG components that are available. How to load and instantiate these 10 FXG components?

       

      (I want to avoid compiling an SWF that would contain 1000 FXG components and thus would be large in file size.)

       

      Here are snippets from an example which can instantiate FXG components, but would probably be too large in SWF size if would contain all the FXG components.

       

      fxgstuff/rect.fxg and fxgstuff/circle.fxg contain this:

       

      <fxg:Graphic xmlns:fxg="http://ns.adobe.com/fxg/2008" version="1">  

         <fxg:Group>

            ...

       

      Snippets from the main MXML file:

       

        ...

       

        import fxgstuff.*;

        import spark.core.SpriteVisualElement;

       

        ...

       

        public function addRect():void {

          ...

          var _rect:SpriteVisualElement = new rect;

          _rect.x = _x_rect;

          _rect.y = _y_rect;

          container.addElement(_rect);

        }

       

        public function addCircle():void {

       

          ...

       

        <s:Button label="Add rectangle" click="addRect()"/>

        <s:Button label="Add circle" click="addCircle()"/>

        <s:Group id="container"/>

       

        ...

       

      Tobi

        • 1. Re: How to load and instantiate FXG at runtime?
          tobi3 Level 1

          P.S.

           

          The 1000 FXG components would be available on the server, but the Flash app wouldn't contain them. The app would just load the ones the user wants.

          • 3. Re: How to load and instantiate FXG at runtime?
            vivoices Level 1

            I would make the effort of putting each graphic into its own MXML Module and load whatever is needed.

             

            David

            • 4. Re: How to load and instantiate FXG at runtime?
              tobi3 Level 1

              But then the size of the Flash app (SWF) will be quite large, right? (If I have hundreds of FXG graphics.)

              • 5. Re: How to load and instantiate FXG at runtime?
                vivoices Level 1

                You put each graphic into a MXML Module (not MXML Component) which stays on the server and is loaded at runtime when you request it, as in:

                // >>>>>  Imports  <<<<<

                import flash.events.Event;

                import flash.net.URLLoader;

                import flash.net.URLLoaderDataFormat;

                import flash.net.URLRequest;

                 

                import mx.events.ModuleEvent;

                import mx.modules.IModuleInfo;

                import mx.modules.ModuleManager;

                 

                 

                 

                // >>>>>  Variable Declarations  <<<<<

                 

                private var urlLdl:URLLoader;

                private var modInfo:IModuleInfo;

                private var oneGraphicsModule:Object;

                 

                 

                // >>>>>  Methods  <<<<<

                 

                private function moduleLoader( pathToModule:String ):void

                {

                  // Loading one Module

                  modInfo = ModuleManager.getModule( pathToModule );

                  urlLdl = new URLLoader(  );

                  urlLdl.dataFormat = URLLoaderDataFormat.BINARY;

                  urlLdl.addEventListener( Event.COMPLETE, bytesLoadedHandler );

                  urlLdl.load( new URLRequest( pathToModule ) );

                }

                 

                private function bytesLoadedHandler( event:Event ):void

                {

                  modInfo.addEventListener( ModuleEvent.READY, moduleLoadedHandler );

                  modInfo.load( null, null, event.target.data );

                }

                 

                private function moduleLoadedHandler( event:ModuleEvent ):void

                {

                  oneGraphicsModule = modInfo.factory.create();

                  // Do with the module what you need to . . .

                }

                 

                So all of your graphics stay on the server, you "just" need to put each graphic or group of graphics into a MXML Module.

                 

                 

                 

                David

                1 person found this helpful
                • 6. Re: How to load and instantiate FXG at runtime?
                  tobi3 Level 1

                  Hi David,

                   

                  Thanks!

                   

                  I haven't had a chance to try this out yet, so I can't mark it as "Correct answer", but I marked it as "Helpful" for now - I hope that's OK.

                   

                  Tobi