1 Reply Latest reply on Apr 22, 2008 5:42 PM by jpsoul

    Dynamic code loading

    redrony
      I need to load dynamic code to the application at run time (i don't know the swf url).
      What is the best way to do that?
      i think that mx:Modules is a problem because it can't be done for action script files (.as) and it's swf per module (meaning per file).
      I know that RSL maybe the solution. Can someone confirm?
      I will be happy to view an example that shows how to use the loaded swf?

      i think that i need the swf and not swc. Is this correct?

      Tnx rony
        • 1. Re: Dynamic code loading
          jpsoul Level 1
          This is easier if you are loading AS only created swfs. Just have your class extend sprite so you can export as a swf. You also need to be mindful of the Application Domain. Also, the loading class has to have the appToLoad class in it's build path (right click on a project, select properties, select Flex Build Path, add Folder, select project folder).

          Here's the pseudo code:
          in the app to load:

          public class AppToLoad extends Sprite
          {
          // your implementation here
          }

          then, in the loader app:

          import {package}.AppToLoad
          private function loadIt():void
          {
          ldr = new Loader();
          var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
          ldr.contentLoaderInfo.addEventListener(Event.INIT, handleLoaded);
          ldr.load(new URLRequest("AppToLoad.swf"), ldrContext);
          }

          private function handleLoaded(e:Event):void
          {
          var myLoadedSwf:AppToLoad = LoaderInfo(e.target).content as AppToLoad ;
          }

          I usually use interfaces to hide the implementation:
          AppToLoad:
          import {package}.ISomeInterface
          AppToLoad extends Sprite implements ISomeInterface

          The app doing the loading:
          import {package}.ISomeInterface
          var myLoadedSwf:ISomeInterface = LoaderInfo(e.target).content as ISomeInterface;


          I still haven't figured out how to type cast a loaded mxml compiled swf ;o(