Creating XML-based menus with Flash CS4 and Flex
In this tutorial, i’ll be showing you how to use the Flex Component Kit, XML, Flash CS4 and Flex 3 to build an extensible, aesthetically pleasing and interactive menu system. Each ingredient in this tutorial is of equal important (with the exception of XML which can easily be replaced with JSON, CSV, etc).
First, a preview of what i will be covering in this tutorial:
In the SWF file above, an XML file is being processed and x amount of menu items are being created at runtime according to the XML specification of the menu. In the SWF above, i’m using the following XML file:
1 2 3 4 5 6 7 | <menu> <item label="The Dark Side Of The Moon"/> <item label="Wish You Were Here"/> <item label="Delicate Sound Of Thunder"/> <item label="The Division Bell"/> <item label="Pulse"/> </menu> |
Click here to download the exercise files for this tutorial.
You will also need to download and install the Flex Component Kit from here.
Alright, so let’s dive right in.
So what are we exactly trying to achieve here? We’re trying to:
- take a symbol that we have created in Flash CS4 and export it
- import the symbol into Flex
- create an XML-based menu with instances of the symbol
Step 1
Open the flash folder in the exercise files and open the MenuItem.fla file inside of Flash CS4. (Note: There is nothing Flash CS4-specific in this file, so if you’d like a version of the FLA that’ll work with CS3, pop me an email)
On the stage you will find one symbol – the MenuItem symbol. In the Library, there is a MenuItem symbol that has been exported for Flex, the FlexComponentBase and two other symbols that are used inside the MenuItem symbol.
All that the MenuItem symbol is is a MovieClip with a little animation, a little masking, a dynamic text field and a background MovieClip. The reason why i made the background its own symbol is because you may want to do a color transform tween or something once you roll over the MenuItem symbol. I have not done this in the tutorial, but nonetheless – give it a go if you feel the need.
Once you have installed the Flex Component Kit, you will see that two new items are available in the Commands menu: Convert Symbol to Flex Component and Convert Symbol to Flex Container. Once you have created a symbol inside of Flash, you will select it and select the Convert Symbol to Flex Component from the Commands menu. This will do all the heavy lifting that is required to make it compatible with Flex.
There are a couple of things to note before you export the symbol:
Firstly, you will need to export your symbol for ActionScript by right-clicking the symbol in the library and selecting Properties. Check the box next to Export for ActionScript. Next, choose a name for your symbol to be used in Flex. I have chosen MenuItem as the Class, and i have chosen MenuItemClass as the Base Class.
I will be coming back to this in a moment. For the moment, you can set the Base Class value to mx.flash.UIMovieClip – this class is a modified implementation of the usual MovieClip class you use in Flash, and will make your symbol compatible with Flex. Now select the Convert Symbol to Flex Component option from the Commands menu.
Step 2
Now we’re going to import the newly prepared Flash symbol for Flex. Flex makes use of .SWC files when importing external libraries, so we’re going to export our Flash symbol to a .SWC file. We do this by publishing the FLA. Go over to Flash and hit Shift+F12, or select Publish from File->Publish. This will export our symbol as a SWC file, as well as a SWF file. We will not be using the SWF file.
Now, go over to Flex and import the project in the exercise files. Go to File->Import->Flex Project, and navigate to the path where you exported the exercise files.
Create the Flex project and then go to the Project menu, and select Properties. Find the Flex Build Path option and select the Library Path tab. In this tab, click on the Add SWC button. Navigate to the flash folder in the project and find the SWC file you have recently published.
Now you’re ready to use your symbol inside of Flex! Remember the name of the class you specified in your symbol linkage in Flash? I used MenuItem. Go over to your main application file in Flex, open a new MXML tag and start typing MenuItem. You will see that Flex picks up your Flash symbol in the MXML declaration. Now we’re ready for some real action.
Step 3
As you can see from my Flex example, i am loading in an XML file called structure.xml. This XML file contains a very basic structure – it’s merely 5 items, each with label attributes – and all relating to one of the greatest bands i’ve had the pleasure of listening to… Pink Floyd
If you open up the flash folder of the project, you will find a MenuItemClass.as file. This will act as the base class for your Flash symbol. Go back to Flash CS4, right click on your symbol in the library and click Properties. Now set the Base Class value to MenuItemClass.as. Here’s an explanation of the MenuItemClass.as file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package { import mx.flash.UIMovieClip; // UIMovieClip extends the functionality of the standard flash.display.MovieClip class, so you can use play(), gotoAndStop(), etc to control your Flash symbol's timeline like you normally would in Flash public class MenuItemClass extends UIMovieClip { public function MenuItemClass() { super(); // Inheret the super-class' properties and functions } // Setter function for the label dynamic textfield public function set label(label:String):void { textField.text = label; } // Getter function for the label dynamic textfield public function get label():String { return textField.text; } } } |
Once you have changed your Flash symbol in Flash CS4, remember to re-publish it so that the SWC will be updated in Flex.
Here’s an explanation of the logic in the Menu.mxml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | private function createMenu():void { var items:XMLList = menuXML..item; // Search for all items in the XML for each(var item:XML in items) // Loop through all items { var menuItem:MenuItem = new MenuItem(); // Create new instance of the Flash symbol we imported menuItem.label = item.@label; // Set the text property of the dynamic text field to the label of the current item in the XML menuItem.height = 45; menuItem.xml = item; // Add the XML of the current item to the xml property of the menuItem variable. Note: this can be named anything since MenuItem's superclass is UIMovieClip which is a dynamic class. You could use menuItem.myBigToe = item if you liked addChild(menuItem); // Add the item to the stage menuItem.addEventListener(MouseEvent.CLICK, showDetails); } } |
And that’s it! That’s how you can use Flash symbols inside of Flex.
| Print article | This entry was posted by Danny Kopping on March 17, 2009 at 10:49 pm, and is filed under ActionScript, Flash, Flex, Lessons. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
Truly Cached Flex Modules
about 7 months ago - 3 comments
I was working on a project recently and I discovered (or possibly misunderstood – see disclaimer) that Flex does not cache modules correctly, or – at the very least – it does not do it effectively according to my tests.
Using a Custom Cursor in Flex with CSS
about 8 months ago - No comments
Learn how to define a custom Flex busy cursor in pure CSS
Converting GIF to SWF using Flash CS4
about 8 months ago - 2 comments
A typical ballache… You want to use one of the cool loaders from http://ajaxload.info/ in your Flash/Flex apps, but it only generates a GIF file…
QuickTip: asSQL Connection Problem with non-localhost hostname
about 8 months ago - No comments
Read this post to fix that annoying error with asSQL when trying to connect to remote MySQL installations
Fixing that annoying feature of List controls
about 9 months ago - No comments
Learn how to fix that ever-painful “optimization” feature of Flex’s list components
The Whirlwind that is Adobe in 2009
about 10 months ago - 1 comment
It’s been quite a fascinating year thus far; Adobe has really started getting serious about Flash, Flex and the developers involved
Flex font embedding nightmare
about 12 months ago - No comments
There are many things that really irritate me – pretence, dishonesty, blind faith and Flex font embedding! Sometimes it makes me wonder whether i really exist… Ok, that’s taking it a bit far, but it really grates my nuts sometimes
Flash & Flex Developer's Magazine
about 1 year ago - No comments
The new version of FFDMag is officially out! The publication is now an online-based magazine, available free of charge to all that want to check it out. Check out my article about Flex & AMFPHP integration on Page 54.
Runtime Shared Libraries – indispensable and infuriating!
about 1 year ago - 2 comments
Utilizing the RSL (Runtime Shared Library) mechanism in Flex is arguably one of the quickest and easiest ways to drastically shrink your Flex application’s file size. The RSL mechanism essentially allows you to compile your Flex application without embedding the Flex framework into the code. So, if the Flex framework isn’t compiled into the Flex


about 1 year ago
hectic man. sooo cool.
about 1 year ago
Great example, very clear! Thanks!
Do you have any experience with extending the ContainerMovieClip Class, or using it with the UIMovieClipClass? I am having issues with the FlexContentHolder, as soon as I compile more then one component.
about 1 year ago
I just did find a workaround, or the solution?
I need to compile all components into the same swc!
Any comments?
about 1 year ago
lunacafu – if you put all your flash symbols in one FLA, then they will export into one SWC file. Google for tutorials on skinning buttons to see what I mean.
When you skin buttons, you put 4 symbols in one file and compile that file into one SWC that contains all 4 skins.
about 6 months ago
Hiya this looks fantastic but am getting a 404 on the exercise file zip. Has it moved? thx
about 6 months ago
Hey Bruce
Thank you very much for pointing this out
I’ve fixed the link.
Thanks for reading. Hope this post helps you…