Truly Dynamic Flash Content
by leon on February 15, 2008
This post is inspired by a recent project that I have been working on. Imagine the scenario:
You have one Flash file that you would like to sit in two or more locations that have different styles (colours, fonts, etc). You also require that the Flash should take on the style of its current location automatically?
So how do you do it? Our old friend XML of course, and some fancy functions with the string class.
The idea is simple, but requires some forward planning in terms of file structure and naming conventions. This way you can use the _URL method in Flash to determine the file location. You can then parse this string and do a number of functions to it.
For best practice follow a structure such as:
www.mysite.co.uk/content/SPECIFIC_LOCATION_NAME/flash/FLASH_NAME.swf
In the above example, it would help no name your Flash file with a consistent format, that is always the same length. This way you can use the string class to count that number of characters from the end of the file name (plus the word flash) and perform a split at the / character. You can then count back from that slash until you see another slash and save that string to a variable name.That variable name should now be equal to the SPECIFIC_LOCATION_NAME. Are you with me so far? For more information about the string class in Flash, head here.
If you have followed the structure above, you can now have a folder at the same level of the Flash folder called XML, which can contains an XML file, with properties specific to each location. (each XML file should have the same file name, you only need to change the value for SPECIFIC_LOCATION_NAME).
Back inside Flash you could build an XML file path by using the SPECIFIC_LOCATION_NAME variable that we created before.
You can now parse the XML file and apply the various colours to specific/generic areas in your Flash file.
Ofcourse you will need to know how to parse XML into Flash, but a quick hunt around on the net will throw up a load of answers.
If all of this sounds a bit too extreme, you could of course have separate Flash files for each location and manually change the colours and assets, but this way will allow you to use the same Flash file in multiple places by simply copying the SWF. There is no need to even open up the FLA file.
As soon as I get round to it, i’ll build a simplified version and get it up online. (The project that I initially built it for was so complex, it could have an entire Blog devoted to it).
Another method is to use FlashVars to pass the XML file name/path into Flash. You could then dynamically create the value for the FlashVars using your preferredĀ programming language (PHP, ASP, ASP.NET, etc). Both methods give the same outcome.
Tips!
A few tips for working with strings:
- Always convert your string to lower case before you try to do a search on it – this was avoid errors with mixed case file locations.
- Have a fall back method incase the XML fails to load. There is nothing worse than a user seeing the wrong layout. As far as they are concerned everything works, even if it wasn’t meant to!

Leave your comment