// From www.flashdev.ca/article/building-a-basic-menu-in-actionscript-30-tutorial/ // Loaded by Mark Bell, 20 Dec 2007 WORKING COPY package ca.flashdev.example { import flash.display.Sprite; import flash.display.Shape; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.net.URLRequest; import flash.net.navigateToURL; public class BasicMenu extends Sprite { // Create the arrays of button names and . private var __MenuArray:Array = new Array("Mr. Bell", "Mr. Sparks", "Mr. Wiseman", "PwrSchool"); private var __MenuURLs:Array = new Array("http://www.csun.edu/~mb326958", "http://nhps.us/sparks", "http://nhps.us/wiseman", "http://newheightsprep.org:8080/public"); /** * The constructor. This method is fired automatically when the class is instantiated. */ public function BasicMenu():void { drawMenu(); } /** * Draw the menu. */ private function drawMenu():void { // These variables will hold the x and y positions of the next button in the menu. var xPos:Number = 230; var yPos:Number = 80; // Create a holder that will contain the menu. var menuHolder:Sprite = new Sprite(); // Add the holder to the stage. addChild(menuHolder); // Create text formatting for the text fields in the menu. var format:TextFormat = new TextFormat(); format.font = "Verdana"; format.color = 0x000000; format.size = 12; format.bold = true; // Loop through the array. Create each item listed in the array. for (var i in __MenuArray) { // Create the integer counter to pass to the functions. var k:int = 0; k = k+i; trace(k); // Create the button. var button:Sprite = new Sprite(); button.name = "button"+i; // Disable the mouse events of all the objects within the button. button.mouseChildren = false; // Make the sprite behave as a button. button.buttonMode = true; // Create the label for the down button state. var label:TextField = new TextField(); label.autoSize = TextFieldAutoSize.LEFT; label.selectable = false; label.defaultTextFormat = format; label.text = __MenuArray[i]; // Create an up state for the button. var up:Sprite = new Sprite(); up.graphics.lineStyle(1, 0x000000); up.graphics.beginFill(0x00FF00); up.graphics.drawRect(0, 0, 100, 30); up.name = "up"; // Create an over state for the button. var over:Sprite = new Sprite(); over.graphics.lineStyle(1, 0x000000); over.graphics.beginFill(0xFFCC00); over.graphics.drawRect(0, 0, 100, 30); over.name = "over"; // Add the states and label to the button. button.addChild(up); button.addChild(over); button.addChild(label); // Position the text in the center of the button. label.x = (button.width/2) - (label.width/2); label.y = (button.height/2) - (label.height/2); // Add mouse events to the button. button.addEventListener(MouseEvent.MOUSE_OVER, displayActiveState); button.addEventListener(MouseEvent.MOUSE_OUT, displayInactiveState); button.addEventListener(MouseEvent.CLICK, displayMessage); // Add the button to the holder. menuHolder.addChild(button); // Position the button. button.x = xPos; button.y = yPos; // Increase the x position for the next button. xPos += button.width + 2; // Hide the over state of the button. over.alpha = 0; } // Postion The Menu. menuHolder.x = 20; menuHolder.y = 20; } /** * Show the active state of the button. */ private function displayActiveState(event:MouseEvent):void { // Hide the over state of the button. event.currentTarget.getChildByName("over").alpha = 100; } /** * Hide the active state of the button. */ private function displayInactiveState(event:MouseEvent):void { // Show the over state of the button. event.currentTarget.getChildByName("over").alpha = 0; } /** * Display a message in the Output window. */ private function displayMessage(event:MouseEvent):void{ // Output the name of the clicked button. //var gotoMB_URL:URLRequest = new URLRequest ("http://www.csun.edu/~mb326958"); var i:int; var gotoMB_URL:URLRequest = new URLRequest (__MenuURLs[i]); navigateToURL(gotoMB_URL); trace(event.currentTarget.name); trace(i); } } /*} var gotoMB_URL:URLRequest = new URLRequest ("http://www.csun.edu/~mb326958"); var gotoMS_URL:URLRequest = new URLRequest ("http://nhps.us/sparks"); var gotoMW_URL:URLRequest = new URLRequest ("http://nhps.us/wiseman"); var gotoNH_URL:URLRequest = new URLRequest ("http://newheightsprep.org:8080/public"); function MrBellbuttonClick (myevent:MouseEvent):void { navigateToURL(gotoMB_URL); }; function MrSparksbuttonClick (myevent:MouseEvent):void { navigateToURL(gotoMS_URL); }; function MrWisemanbuttonClick (myevent:MouseEvent):void { navigateToURL(gotoMW_URL); }; function NHPSbuttonClick (myevent:MouseEvent):void { navigateToURL(gotoNH_URL); }; MrBell_btn.addEventListener(MouseEvent.CLICK,MrBellbuttonClick); MrSparks_btn.addEventListener(MouseEvent.CLICK,MrSparksbuttonClick); MrWiseman_btn.addEventListener(MouseEvent.CLICK,MrWisemanbuttonClick); NHPS_btn.addEventListener(MouseEvent.CLICK,NHPSbuttonClick); */ }