Flash CS3 XML Menu, ActionScript 3.0
/**************************************
Developed by Anil kumar N D
anilkumarnd@gmail.com
Flash CS4 professional
ActionScript 3.0
14 October,2009
http://flash-workshop.blogspot.com/
**************************************/
package {
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.MouseEvent;public class xmlMenu extends MovieClip {
private var mc_menuHolder:MovieClip;
private var xml_Object:XML;
private var ldr_loader:URLLoader;
private var uin_i:uint;
private var txt_status:TextField;
private var arr_menuItem:Array;
private var unt_currentID:uint;
private var mc_currentMenu:MovieClip;
/***********************************************************************************************
Default constructor
************************************************************************************************/
public function xmlMenu():void {
stage.frameRate = 30;
XML.prettyPrinting = false;
arr_menuItem = new Array();
ldr_loader = new URLLoader();
ldr_loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
ldr_loader.load(new URLRequest("xmlMenu.xml"));
txt_status = new TextField();
addChild(txt_status);
txt_status.y = 200;
txt_status.width = 300;
}
/***********************************************************************************************
This function is an event handler for complete event of ldr_loader
After that it will build the menu and its listeners.
INPUT :
IN_Event - Event
************************************************************************************************/
private function loaderCompleteHandler(IN_Event:Event):void {
mc_menuHolder = new MovieClip();xml_Object = XML(ldr_loader.data);
uin_i = 0;
for each (var item:XML in xml_Object..mainMenu) {
var mc_menu:MovieClip = createMenuItem(30,100,0xFF9900,5);
TextField(mc_menu.getChildAt(0)).htmlText = item.caption;
mc_menu.id = uin_i;
mc_menu.control = "Menu";
mc_menu.x = (uin_i)*mc_menu.width;
mc_menu.addEventListener(MouseEvent.CLICK,clickHandler);
mc_menu.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
mc_menu.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
var mc_subMenu:MovieClip = new MovieClip();
mc_subMenu.name = "mc_subMenu_"+uin_i++;
for(var uin_j:uint = 0 ; uin_j<item.item.length();uin_j++){
var mc_menuItem:MovieClip = createMenuItem(30,100,0xFFFFCD,5);
TextField(mc_menuItem.getChildAt(0)).htmlText = item.item[uin_j].caption;
mc_menuItem.y = (uin_j+1)*mc_menuItem.height;
mc_menuItem.addEventListener(MouseEvent.CLICK,clickHandler);
mc_menuItem.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
mc_menuItem.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
mc_subMenu.addChild(mc_menuItem);
}
mc_subMenu.visible = false;
mc_subMenu.x = mc_menu.x;
mc_menuHolder.addChild(mc_subMenu);
mc_menuHolder.addChild(mc_menu);
}
addChild(mc_menuHolder);
mc_menuHolder.x = (stage.stageWidth/2) - (mc_menuHolder.width/2);
mc_menuHolder.y=10;
}
/***********************************************************************************************
This function is an event Handler for click event on all menus and its sub menus
INPUT :
IN_Event - MouseEvent
************************************************************************************************/
private function clickHandler(IN_Event:MouseEvent):void{
//trace(TextField(IN_Event.target.getChildAt(0)).text);
txt_status.text = "You have selected "+TextField(IN_Event.target.getChildAt(0)).text;
if(IN_Event.target.control == "Menu"){
unt_currentID = IN_Event.target.id;
mc_currentMenu = MovieClip(IN_Event.target);
mc_menuHolder.getChildByName("mc_subMenu_"+unt_currentID).visible = true;
arr_menuItem.splice(0,arr_menuItem.length);
for(var uin_j:uint = 0 ; uin_j<MovieClip(mc_menuHolder.getChildByName("mc_subMenu_"+unt_currentID)).numChildren;uin_j++){
arr_menuItem.push(MovieClip(mc_menuHolder.getChildByName("mc_subMenu_"+unt_currentID)).getChildAt(uin_j));
}
}
}
/***********************************************************************************************
This function is an event Handler for MOUSE_OVER event on all menus and its sub menus
INPUT :
IN_Event - MouseEvent
************************************************************************************************/
private function overHandler(IN_Event:MouseEvent):void{
IN_Event.target.MouseOver = true;}
/***********************************************************************************************
This function is an event Handler for MOUSE_OUT event on all menus and its sub menus
INPUT :
IN_Event - MouseEvent
************************************************************************************************/
private function outHandler(IN_Event:MouseEvent):void{
IN_Event.target.MouseOver = false;
stage.addEventListener(Event.ENTER_FRAME,hideMenu);
}
/***********************************************************************************************
This function ceate for menu objects
INPUT :
int_Height - int
int_width - int
num_color - Number
int_cornerRadius - int
RETURN :
mc_menu - MovieClip
************************************************************************************************/
private function createMenuItem(int_Height:int,int_width:int,num_color:Number,int_cornerRadius:int):MovieClip{
var mc_menu:MovieClip = new MovieClip();
mc_menu.graphics.lineStyle(1,0X000000,1);
mc_menu.graphics.beginFill(num_color,1);
mc_menu.graphics.drawRoundRect(0,0,int_width,int_Height,int_cornerRadius,int_cornerRadius);
mc_menu.graphics.endFill();
var txt_menuItem:TextField = new TextField();
txt_menuItem.height = int_Height;
txt_menuItem.width = int_width;
txt_menuItem.autoSize = TextFieldAutoSize.CENTER;
txt_menuItem.selectable = false;mc_menu.addChild(txt_menuItem);
mc_menu.mouseChildren = false;
mc_menu.buttonMode = true;
return mc_menu;
}
/***********************************************************************************************
This function is an event Handler for ENTER_FRAME event on stage
INPUT :
IN_Event - Event
************************************************************************************************/
private function hideMenu(IN_Event:Event):void{
if(arr_menuItem.length >0){
if(mc_currentMenu.MouseOver == false && !arr_menuItem.some(CheckMouseOver)){
mc_menuHolder.getChildByName("mc_subMenu_"+unt_currentID).visible = false;
}
stage.removeEventListener(Event.ENTER_FRAME,hideMenu);
}
}
/***********************************************************************************************
This function checks for MOUSE_OVER event on every menus and menu items
INPUT :
arr - Array
RETURN :
A Boolean value
************************************************************************************************/
private function CheckMouseOver(element:*, index:int, arr:Array):Boolean {
return element.MouseOver;}
}
}
Comments
Work from home India
flash website developer