Configure your Flash Applications using XML – Part Two

In the previous article on using configuration XML files in Flash we looked at using the XML file to overwrite default values in your Flash application. That’s a great way to get started but there is of course much more you can do with configuration files.

In this article I’ll show you some methods that allow you to change the configuration parameters at runtime and update the XML file using PHP.

$2.89
- OR -

Overview

What have we done so far?

Before we get started building those new methods let’s recap what was discussed in the initial article. First of all we have the config.xml file where parameters are defined. That file looked something like this:

<?xml version="1.0" ?>

<config>
  <param id="companyName" value="DMXzone" />
  <param id="companyLink" value="http://www.dmxzone.com" />
</config>

The id attribute of the param nodes specified the variable name in your FLA and the value attribute, as its name suggests, holds the value of that variable.

What would happen in the FLA is that the configuration XML file gets loaded in and throughout the application a getConfigParam method is used to get the hold of the appropriate value. When a variable is defined both in the FLA and the XML file, the value from the XML file would take precedence.

The getconfigParam method accepts one argument which is the variable name you want to get the value for. Here is what the code for that method looks like:

getConfigParam = function(param:String) {

var tmp = configXML["idMap"][param];

if(tmp == undefined) {

    tmp = this[param];

}

return tmp;

}

In the example I used in the previous article the FLA had three variables defined: companyName, companyLink and videoFile.

var companyName:String = "My Default Company";

var companyLink:String = "http://www.visitmehere.com";

var videoFile:String = "video1.flv";

Because companyName and companyLink are defined in the XML file those two variables would be ignored when using the getConfigParam method. The videoFile variable on the other hand isn’t in the XML file and as a result the default value defined in the FLA is used.

If at any time the videoFile variable needs to be overwritten we could then simply add it to the configuration XML file and have the application working without having to recompile anything.

Peter Elst

Peter ElstPeter 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

See All Postings From Peter Elst >>

Reviews

Be the first to write a review

You must me logged in to write a review.