I have been looking for a better way to handle this issue maybe its just the verbiage I’m using but I’m not finding the answer I want. I’ll explain what I’m doing and hopefully someone out there will be able to explain how to do this.
When developing a flash project on my machine I can use something like:
var request:URLRequest = new URLRequest("images.xml");
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handleResult);
loader.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
loader.load(request);
As long as the images.xml file is located in the same directory as the .fla then it works fine. However once I move the .swf, .xml and my images to the web it all goes down the drain. The .swf can no longer find the .xml file and I know why. Using FlashBug I can read the error in the console.
IOError: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: http://www.mydomain.com/images.xml"]
And there’s the problem when I move my .swf to my web server the URLRequest adds my domain to the request. Since the file was just located in the same directory on my machine it didn’t cause an issue to just say “images.xml” but now in order to make it work I have to change it to “http://www.mydomain.com/flash/myproject/images.xml”.
I want to know if there is a way to force the .swf to just continue to look in the directory it is in for the files it needs. I think having to spell it out is counter-intuitive because then I can’t just move the files without having to re-write my AS3 code.
I know this can be done I’ve seen projects do it, I just wish I knew how they did it.
Yes, there is, you can use the
baseattribute of the object and embed tags.As default, relative paths in ActionScript are resolved relative to the page that contains the Flash, not relative to the swf file, but the
baseattribute lets you specify another directory path, that is used to resolve relative paths.So for your example path above, it would be something like this:
Or if you use SwfObject to embed the swf, you can specify base via the attributes parameter:
http://code.google.com/p/swfobject/wiki/documentation
Edit:
A followup on this, although the answer has already been accepted, but since you comment that you where initially looking to handle it in ActionScript instead of HTML, and for possible future readers:
If you for some reason don’t want to, or can’t, set the
baseattribute in the HTML, an alternative in ActionScript is to useloaderInfo.urlto get the URL of the swf, pick the directory part of that and use it as a path to load files that are next to the swf. This is code copied from a project of mine, including the comments:And later, when you want to load for example an XML file, you would do: