dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Problem with relative path in externally loaded SWF

  1. #1
    Member
    Join Date
    Sep 2007
    Posts
    69

    Problem with relative path in externally loaded SWF

    I have a flash movie (let's call it B.swf) that loads an XML file which is located in the same directory to provide some data. This flash movie works fine itself.

    Now, I've made another flash movie, a main movie (let's call it A.swf), which loads different SWF's in turn, including the aforementioned one B.swf (which now would be located along with the XML file in a subdirectory).

    To my surprise, I've checked that now I get an error, as the path I specified for the XML file in B.swf is not valid anymore when B.swf is loaded from A.swf. The relative path from which it's searching for the file is the directory where A.swf is located, not the one where B.swf is located, as happened when this movie was executed standalone.

    Is there any way to fix this so that I don't have to change the path I specified in the code and I can use B.swf either way externally loaded or standalone?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You can have B check its loaderInfo's url property and construct an absolute URL to the xml using that.

  3. #3
    Member
    Join Date
    Sep 2007
    Posts
    69
    I'm not sure what you mean. You have to specify a URL before loading.

    This is what I have in the secondary movie (b.swf):

    var externalData:URLLoader = new URLLoader(new URLRequest("data.xml"));

    these would be the paths from the main movie:
    a.swf
    assets/b.swf
    assets/data.xml

    I'd like to avoid having to change the URL to new URLRequest("assets/data.xml")

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    B itself has a loaderInfo property, all DisplayObjects do. So before loading, you can check that, excise the "b.swf" part, and put in "data.xml". Viola, you have an absolute URL to the correct path.

    Code:
    var myPath:String = loaderInfo.url;
    var dataPath:String = myPath.substring(0, myPath.lastIndexOf("/"))+"data.xml";
    var externalData:URLLoader = new URLLoader( new URLRequest(dataPath));
    I may have an off-by-one error in there regarding indices, but you get the idea.

  5. #5
    Member
    Join Date
    Sep 2007
    Posts
    69
    Thank you, now I get it.

    Also, once the xml is loaded, since the xml document contains paths to upload images for a gallery, I guess I'll have to do the same with those paths when I retrieve the xml data. Is this correct?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If those paths are relative, yes.

  7. #7
    Member
    Join Date
    Sep 2007
    Posts
    69
    ok, thank you again! I get the point.

  8. #8
    Member
    Join Date
    Sep 2007
    Posts
    69

    Preloading issues

    edit: repeated
    Last edited by Jazztronik; 10-15-2009 at 10:55 AM. Reason: repeated

  9. #9
    Junior Member
    Join Date
    Oct 2009
    Posts
    1
    I am running into the same problem. I have worked with xml strings before but never created an xml file until today and having trouble as well with receiving errors because I'm loading an external swf from a subdirectory folder with xml file in said folder along with jpegs.

    I will test your solution but my only question is how do I write in the paths to my files in the xml file itself? I figured out that that might be part of the issue on why I'm receiving error message but I don't understand how to make that change. And I know it's probably a simple solution but I'm a perplexed newbie on this issue :/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center