Wednesday, September 16, 2009

Flash ActionScript 2.0 Dynamic Vertical Menu

we all know about XML and its uses over Internet. Now a days, requirements comes mostly for dynamically created flash , i mean to say xml embeded flash file. As for now I am going to show how to create such files. In my example I would show how to create menu item dynamically, embeding XML with flash. So lets start....

1) First we need to create a XML file maintaining this following formate, you can know more about XML from this link http://www.w3schools.com/xml/default.asp . My XML file should look like this.


2) Open Flash proffesional 8 or upperversion, create a new file, save the file with the XML file you got.

3) Create a rectangel , width:120, and height:20, and then convert it into movieclip and name it as "menuitem"

4) Double click on this particular movieclip and create a new layer, go to the first frame of the new layer and then create a dynamic text field and give it an instance name "name"

5) Come over to the stage and press CTLR + L or you can go to windows>library. you can find the movieclip named menuitem there, right click over the movieclip, chose properties, there yon find Linkage option.

Check for Export for Actionscript, the name for Identifier and Export for first frame will get filled, dont change them, keep it as it is. Press ok and then come to the stage. you can find the menuitem movieclip, remove it from the stage.

6) Select the frist frame and press F9, the action panel will open up. From here on we continue with the actionscript 2.0. Generates a list of menu items , given the inputted parameters. This makes the main menu.

Loading and Interpreting XML

xmlNode = xmlObject.firstChild;
for (var i = 0; i <>
xmlNode.childNodes[i].nodeName;
xmlNode.childNodes[i].attributes.name;
xmlNode.childNodes[i].attributes.action;
xmlNode.childNodes[i].attributes.variables;
}

Get XML

menu_xml = new XML();
menu_xml.ignoreWhite = true;
menu_xml.onLoad = function(ok){
if (ok){
Createmainmenu(10, 10, 0, this);
}
};
menu_xml.load("myXMLfile.xml");

Generating Menu

if (node_xml.childNodes[i].nodeName == "menu"){
curr_item.node_xml = curr_node;
curr_item.onRollOver = curr_item.onDragOver = function(){
var x = this._x + this._width - 5;
var y = this._y + 5;
GenerateMenu(curr_menu, "submenu_mc", x, y, 1000, this.node_xml);
};
}else{
curr_item.arrow._visible = false;
curr_item.onRollOver = curr_item.onDragOver = function(){
curr_menu.submenu_mc.removeMovieClip();
};
}

Action

Actions = Object();
Actions.gotoURL = function(urlVar){
getURL(urlVar, "_self");
};

At the End the Script will look like this

GenerateMenu = function(container, name, x, y, depth, node_xml) {
var curr_node;
var curr_item;
var curr_menu = container.createEmptyMovieClip(name, depth);
for (var i=0; i<4;>
curr_item = curr_menu.attachMovie("menuitem","item"+i+"_mc", i);
curr_item._x = x;
curr_item._y = y + i*curr_item._height;
curr_item.trackAsMenu = true;
curr_node = node_xml.childNodes[i];
curr_item.action = curr_node.attributes.action;
curr_item.variables = curr_node.attributes.variables;
curr_item.name.text = curr_node.attributes.name;
curr_item.onRollOut = curr_item.onDragOut = function(){
var col = new Color(this.background);
col.setTransform({ra:100,rb:0,ga:100,gb:0,ba:100,bb:0});
};
curr_item.onRelease = function(){
Actions[this.action](this.variables);
CloseSubmenus();
};
}
};

Createmainmenu = function(x, y, depth, menu_xml){
GenerateMenu(this, "mainmenu_mc", x, y, depth, menu_xml.firstChild);
mainmenu_mc.onMouseUp = function(){
if (mainmenu_mc.submenu_mc && !mainmenu_mc.hitTest(_root._xmouse, _root._ymouse, true)){
CloseSubmenus();
}
};
};
CloseSubmenus = function(){
mainmenu_mc.submenu_mc.removeMovieClip();
};

Actions = Object();
Actions.gotoURL = function(urlVar){
getURL(urlVar, "_self");
};
Actions.newMenu = function(menuxml){
myXMLfile_xml.load(menuxml);
};

myXMLfile_xml = new XML();
myXMLfile_xml.ignoreWhite = true;
myXMLfile_xml.onLoad = function(ok){
if (ok){
Createmainmenu(10, 10, 0, this);
trace( "message area");
}else{
trace("error: XML not successfully loaded");
}
};
myXMLfile_xml.load("myXMLfile.xml");

No comments:

Post a Comment