Local vs. Remote Paths
Normally when I'm working I tend to use XML a lot. Also, my file structuring goes something like this:
- deploy
- swf
- images
- xml
- scripts
- etc.
This is generally how I structure my deploy folder and inside of the root deploy folder I have my .html (or whatever) files. This poses a bit of a problem when I'm loading stuff into the SWF, i.e. external assets such as JPGs, MP3s, or what not. This is because when the files are on your computer and you're testing through the Flash IDE, the SWF is in one folder, but the assets are a folder up and in another asset folder. When you are running it on a server, however, the assets path is relevant to the HTML file which hangs out in the root folder and not the SWF which is embedded from a different, swf folder, so the pathing does not need to go up a folder and drill down into an asset folder.
There is a very simple and elegant solution to this that Marie De Luna pointed out to the Flash dev team at Razorfish. This one line of code gives you a lot of flexibility. Below is what I used to normally do when I needed to flip back and forth between a debug mode and when I put the files up on the server.
-
var debug:Boolean = true;
-
-
if (debug)
-
{
-
// in debug mode, set paths
-
var basePath:String = "../";
-
}
-
else
-
{
-
// in site mode, set paths
-
var basePath:String = "";
-
}
That approach works fine and dandy, but you have to remember to turn debug = false everytime you are ready to upload your file and then back to true everytime you want to edit it. Here is the code that makes life easier:
-
var isSWFinHTML:Boolean = (_level0._URL.indexOf("file:///") == -1);
-
-
if (isSWFinHTML)
-
{
-
// swf is online, set paths
-
var basePath:String = "";
-
}
-
else
-
{
-
// swf is local, set paths
-
var basePath:String = "../";
-
}
This will automatically make that check for you so that you don't have to keep toggling it on and off manually. Basically, it checks to see if the file is on the local file system, and if it is, then it knows that the basePath variable will have to go up one folder to access the files that are in the root directory.
This is also very handy if you are using FlashVars, as you can set default values for local testing inside of Flash, like so:
-
var isSWFinHTML:Boolean = (_level0._URL.indexOf("file:///") == -1);
-
-
if (!isSWFinHTML)
-
{
-
// swf is local, set substitute flash vars
-
var basePath:String = "../";
-
var numPages:String = "3";
-
var referURL:String = "http://www.reintroducing.com";
-
// etc...
-
}
This has proven very handy for me over the course of my work for the past year. It saves me having to manually toggle a variable and then upload something that is set to debug and wonder why it isn't working online.
Don't forget then, though, to prefix your variables with the basePath variable, like so (this is an example of an XML parser I used on a project):
-
public function parse($xml:XML):Array
-
{
-
this._XML = $xml;
-
-
this._basePath = (Application.getInstance().timeline.isSWFinHTML) ? "" : "../";
-
-
var workNodes:Array = XPathAPI.selectNodeList(this._XML, "/*/work/project");
-
var numProjects:Number = workNodes.length;
-
var data:Array = new Array();
-
var workVO:WorkVO;
-
var projectNode:XMLNode;
-
-
// get the data
-
for (var i:Number = 0; i <numProjects; i++)
-
{
-
projectNode = workNodes[i];
-
-
workVO = new WorkVO();
-
workVO.name = projectNode.attributes.name;
-
workVO.type = projectNode.attributes.type;
-
workVO.category = projectNode.attributes.category;
-
workVO.src = this._basePath + projectNode.attributes.src;
-
workVO.highSrc = this._basePath + projectNode.attributes.highSrc;
-
workVO.duration = projectNode.attributes.duration;
-
workVO.description = XPathAPI.selectSingleNode(projectNode, "/*/description").firstChild;
-
-
data.push(workVO);
-
}
-
-
return data;
-
}
Written a little differently than the examples I provided, but yields the same result. Hope this helps anyone who many structure similarly to me!