User Guide

546 Chapter 6: Components Dictionary
To use XML data from a server to create and populate a menu:
1.
Select File > New and create a Flash document.
2.
Drag the Menu component from the Components panel to the Stage and delete it.
This adds the Menu component to the library without adding it to the application. Menus are
created dynamically through ActionScript.
3.
In the Actions panel, add the following code to the first frame to create a menu and add
some items:
var myMenu = mx.controls.Menu.createMenu();
// Import an XML file
var loader = new XML();
loader.menu = myMenu;
loader.ignoreWhite = true;
loader.onLoad = function(success) {
// When the data arrives, pass it to the menu
if(success) {
this.menu.dataProvider = this.firstChild;
}
};
loader.load(url);
Note: The menu items are described by the children of the XML document’s first child.
4.
Select Control > Test Movie.
To use a well-formed XML string to create and populate a menu:
1.
Select File > New and create a Flash document.
2.
Drag the Menu component from the Components panel to the Stage and delete it.
This adds the Menu component to the library without adding it to the application. Menus are
created dynamically through ActionScript.
3.
In the Actions panel, add the following code to the first frame to create a menu and add
some items:
// Create an XML string containing a menu definition
var s = "";
s += "<menu>";
s += "<menuitem label='Undo' />";
s += "<menuitem type='separator' />";
s += "<menuitem label='Cut' />";
s += "<menuitem label='Copy' />";
s += "<menuitem label='Paste' />";
s += "<menuitem label='Clear' />";
s += "<menuitem type='separator' />";
s += "<menuitem label='Select All' />";
s += "</menu>";
// Create an XML object from the string
var xml = new XML(s);
xml.ignoreWhite = true;
// Create a menu from the XML object's first child
var myMenu = mx.controls.Menu.createMenu(_root, xml.firstChild);
4.
Select Control > Test Movie.