Using the menu component – part 1.

Earlier I had a request from a user for a Menu component example, because they were trying to create a menu and running into some problems. Given that it seemed like an area that did need a bit more “coverage”, the following examples might help.

Now, there are two ways you can work with the menu components – using inline or external XML. The following example shows how you can load Menu items based on an external XML file. Next blog, inline.

For the set up, do the following.

1. Add an instance of the MenuBar component to the Stage.
2. Give it the instance name myMenuBar (in hurry, forgot to use a suffix).
3. Add the following code to Frame 1 of the Timeline.

myMenuBar.setStyle(“fontSize”, 10);
var ds:XML = new XML();
ds.ignoreWhite = true;
ds.onLoad = function(success:Boolean) {
if (success) {
myMenuBar.dataProvider = ds.firstChild;
var menuListener:Object = new Object();
menuListener.change = function(evt:Object) {
switch (evt.menuItem.attributes.instanceName) {
case “file_new” :
trace(“File > New”);
case “file_open” :
trace(“File > Open”);
case “file_openFromSite” :
trace(“File > Open from Site”);
case “file_openRecent” :
trace(“File > Open Recent”);
case “file_openRecent_01” :
case “file_openRecent_02” :
case “file_openRecent_03” :
case “file_openRecent_04” :
case “file_openRecent_05” :
case “file_openRecent_06” :
case “file_openRecent_07” :
case “file_openRecent_08” :
trace(“File > Open Recent > “+evt.menuItem.attributes.fileName);
case “file_close” :
trace(“File > Close”);
case “file_closeAll” :
trace(“File > Close All”);
default :
trace(“unknown instance name for: “+evt.menuItem.attributes.label);
myMenuBar.addEventListener(“change”, menuListener);

Before you test your code, save the following XML file into the same directory as the FLA: Or, you can target this XML file instead.

That’s it!