Previous Tip  |  Next Tip  |  Design Tips   | [Bill's Home]

[Expand] This is a brilliant tip that I've just discovered, and expands on the reuse theme. I've been experimenting with creating generic components in Flash, and I've developed a simple method of creating components which are identical, but can be changed using XML. Let's take a simple Flash component for a multiple choice question:

The code that is inserted into the component includes a part which reads the URL of the Flash component, and determines the name of the component. Next this name can be converted into a file name with a .xml extension, with:

var infile;


getfilename();

urlXML = new XML();
urlXML.onLoad = convertXML;


if (!urlXML.loaded) {
urlXML.load(infile);
trace("Loaded from local file");
if (!urlXML.loaded) {
question.title("XML file not found");
}
}
trace("XML LOADED");


function getfilename()
{
str=_root._url;

i=str.lastIndexOf('/');
j=str.length;
str=str.slice(i+1,j);
i=str.lastIndexOf('.');
str=str.slice(0,i);
infile=str.concat(".xml");
trace(infile);
}

Now, all we have to do is to copy the test01.swf file into test02.swf, test03.swf, and so on. The test02.swf component will use the test02.xml file, test03.swf will use the test03.xml file, and so on. Thus we have:

Test02.swf (which reads test02.xml):

Test03.swf (which reads test03.xml):

Test04.swf (which reads test04.xml):

Honest. I've used the same Flash file for all of these movies. All that I have done is to change their names.