Tuesday, February 8, 2011

Flex: Loading assets into externally loaded modules...

So, I have Flex project that loads a Module using the ModuleManager - not the module loader. The problem that I'm having is that to load an external asset (like a video or image) the path to load that asset has to be relative to the Module swf...not relative to the swf that loaded the module.

The question is - How can I load an asset into a loaded module using a path relative to the parent swf, not the module swf?

  • You can import mx.core.Application and then use Application.application.url to get the path of the host application in your module and use that as the basis for building the URLs.

    For help in dealing with URLs, see the URLUtil class in the standard Flex libraries and the URI class in the as3corelib project.

    From hasseg
  • Arg! So in digging through the SWFLoader Class I found this chunk of code in private function loadContent:

        // make relative paths relative to the SWF loading it, not the top-level SWF
        if (!(url.indexOf(":") > -1 || url.indexOf("/") == 0 || url.indexOf("\\") == 0))
        {
             var rootURL:String;
             if (SystemManagerGlobals.bootstrapLoaderInfoURL != null && SystemManagerGlobals.bootstrapLoaderInfoURL != "")
                  rootURL = SystemManagerGlobals.bootstrapLoaderInfoURL;
             else if (root)
                  rootURL = LoaderUtil.normalizeURL(root.loaderInfo);
             else if (systemManager)
                  rootURL = LoaderUtil.normalizeURL(DisplayObject(systemManager).loaderInfo);
    
                  if (rootURL)
                  {
                       var lastIndex:int = Math.max(rootURL.lastIndexOf("\\"), rootURL.lastIndexOf("/"));
                        if (lastIndex != -1)
                             url = rootURL.substr(0, lastIndex + 1) + url;
                   }
              }
    }
    

    So apparently, Adobe has gone through the extra effort to make images load in the actual swf and not the top level swf (with no flag to choose otherwise...), so I guess I should submit a feature request to have some sort of "load relative to swf" flag, edit the SWFLoader directly, or maybe I should have everything relative to the individual swf and not the top level...any suggestions?

    hasseg : Everything you said sounds good to an outsider, what you should do here really depends on the project. It's probably preferable to not have to use your own modified SWFLoader, though. In any case, submitting the feature request is a good idea.
    From onekidney
  • You can use this.url in the module and use this as a baseURL.

    var urlParts:Array = this.url.split("/"); urlParts.pop(); baseURL = urlParts.join("/"); Alert.show(baseURL);

    and use {baseURL + "/location/file.ext"} instead of /location/file.ext

0 comments:

Post a Comment