RE: idMap
RE: RE: idMap
Still the same, I tried your suggestion, and your website suggestion for the xml.as file, after that i started F8 again, but still the same, i tried everything, but no luck.....any suggestion?
Kees
If you’re anything like me, you don’t particularly enjoy having to launch Flash for a quick little fix, export again and deploy it on a website.
Well using the power of XML you can make your life a lot easier. What I’ll be discussing in this article is the concept of a configuration XML file and how to use it most efficiently for your Flash projects.
What exactly is a configuration XML file I hear you saying? It’s not difficult at all; in simple terms what it does is define some properties and corresponding values that overwrite the default values in your Flash application.
Let’s look at an example of such an XML file:
<config>
<param id="companyName" value="DMXzone" />
<param id="companyLink" value="http://www.dmxzone.com" />
</config>
The XML file above shows a typical configuration XML file that allows you to change the companyName and companyLink variables without having to recompile the Flash application.
This XML document is saved as config.xml and placed in the root folder of your Flash application. Of course that’s not enough to get it to work; we need a little bit of code in the Flash application to load in the XML and get hold of the configuration parameters which we’ll look at next.
Now we've loaded in the configuration XML file, we need some way to retrieve the parameters. Let's look at the function that does this for us:
getConfigParam = function(param:String) {
var tmp = configXML["idMap"][param];
if(tmp == undefined) {
tmp = this[param];
} else {
tmp = tmp.attributes["value"];
}
return tmp;
}
The idMap property is new in Flash 8 and allows you to easily access XML nodes that have an id attribute defined.
Using the argument that gets passed to the getConfigParam function we check if the parameter is defined in the XML document, if not use the default value stored in the FLA.
This also explains why we need the same variable names in the FLA as the id attributes in the XML file.
Let's see what the initApplication function does that we called after the configuration XML finished loading:
initApplication = function() {
var titleText:String = "<a href=\"";
titleText += getConfigParam("companyLink")+"\">";
titleText += getConfigParam("companyName")+"</a>";
companyName_txt.htmlText = titleText;
loadVideo();
}
In the initApplication function we use the getConfigParam function to retrieve the title and construct a hyperlink to the website defined in the companyLink variable. This hyperlink is then assigned as the htmlText of the companyName textfield.
If the companyLink and companyName variables weren't defined in the configuration XML file what the hyperlink would look like is:
<a href="http://www.visitmehere.com">My Default Company</a>
In this case however both the companyLink and companyName variable are defined in the XML file, so those override the default values and the hyperlink looks like:
<a href="http://www.dmxzone.com">DMXzone</a>
What makes this approach so interesting is that any time you use a variable you might want to change later on without recompiling the application, you call it through the getConfigParam function. If it is defined in the XML it will use that, if the XML for whatever reason failed to load or the parameter isn't defined it will simply use the default one.
One thing to take into consideration is that you have to make a balance between what is useful to put in the configuration XML file and what is not. If you start putting all your variables inside the XML file this will have the negative side effect of having to load a large XML file before starting your application.
Also be sure never to put any sensitive information in the XML file such as passwords or URLs to Flash Remoting gateways or Flash Communication server connections. While that might be good for testing purposes (you can quickly change between different servers without recompiling) it is not really a secure way for deploying your applications to the web.
Peter Elst is a Flash certified professional, Team Macromedia volunteer and runs his own company named MindStudio doing mostly freelance Flash and Flex consultancy, development and training. As a valued contributor to the online Flash community, Peter has presented at numerous international events and conferences and has had his work published in leading magazines and websites.
Over the years the focus of his work changed from interactive animations to multimedia applications, e-learning and content management systems. Peter is user group manager for the MMUG Belgium and blogs on his personal website: www.peterelst.com