Chris Ramsey
Chris Ramsey

Reputation: 49

AS3 SWFs Loading External Files; Change default location

If I have a SWF and an XML file sitting in the same folder, is there a way to get the loader to use the location of the SWF as its starting point for loading the external file (as opposed to the HTML file upon which the SWF plays)? We are copying a group of web sites and some of the paths have unique page codes, but if the SWFs would just look in their own folder they would run fine. :)

Thank you! Chris

Upvotes: 0

Views: 1134

Answers (1)

Josh
Josh

Reputation: 8149

Could try this.

var base:String = unescape( LoaderInfo( this.root.loaderInfo ).url ); // url of SWF

// now we need to remove the SWF name from the url
var lastSlash:uint = Math.max( this.base.lastIndexOf( "\\" ), this.base.lastIndexOf( "/" ) );
return( base.substr( 0, lastSlash + 1 ) );

That will take the url of the SWF, loop through it and find the last "\" or "/" to determine where the swf file name starts, and set the base equal to the path of the SWF.

And then change all relative URLRequests from

var url:URLRequest = new URLRequest( "assets/blah/blah/img.png" );

to

var url:URLRequest = new URLRequest( base + "assets/blah/blah/img.png" );

This would be something I would add as a static property of your application class so that you are only accessing a string and not doing any casting or unicode escaping or anything repeatedly. Just run it once at start up and throw it as a static var because it will never change.

(I did test this and it works, at least locally. I see no reason why it would behave any differently with a URL from the web, however)

Upvotes: 1

Related Questions