/* Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license */ package { import com.yahoo.astra.animation.Animation; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.MouseEvent; import flash.net.URLRequest; import flash.net.navigateToURL; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; [SWF(backgroundColor=0xffffff,frameRate=24)] /** * Demonstrates how to use the Animation utility to create a Flash menu * with items that slide back and forth on rollover. * * Not associated with a FLA file. Meant to be compiled with MXMLC. * * @author Josh Tynjala */ public class SlidingMenu extends Sprite { //-------------------------------------- // Constructor //-------------------------------------- /** * Constructor. */ public function SlidingMenu() { super(); //some simple stage initialization to prevent stretching and strange positioning if(this.stage) { this.stage.scaleMode = StageScaleMode.NO_SCALE; this.stage.align = StageAlign.TOP_LEFT; } //initialize the data that will be used by the menu items this.menuItemData = [ { text: "Yahoo!", url: "http://www.yahoo.com/" }, { text: "Delicious", url: "http://del.icio.us/" }, { text: "Flickr", url: "http://www.flickr.com/" }, { text: "Jumpcut", url: "http://www.jumpcut.com/" }, { text: "Upcoming", url: "http://www.upcoming.org/" }, { text: "Zimbra", url: "http://www.zimbra.com/" } ]; var position:Number = 10; var menuItemCount:int = this.menuItemData.length; for(var i:int = 0; i < menuItemCount; i++) { var itemData:Object = this.menuItemData[i]; var menuItem:Sprite = this.createMenuItem(itemData.text); //associate the menu item sprite with the data object itemData.item = menuItem; menuItem.x = 10; menuItem.y = position; menuItem.addEventListener(MouseEvent.ROLL_OVER, menuItemRollOverHandler); menuItem.addEventListener(MouseEvent.ROLL_OUT, menuItemRollOutHandler); menuItem.addEventListener(MouseEvent.CLICK, menuItemClickHandler); this.addChild(menuItem); position += menuItem.height + 4; } } //-------------------------------------- // Properties //-------------------------------------- /** * @private * Stores the data for the menu items. */ private var menuItemData:Array; //-------------------------------------- // Private Methods //-------------------------------------- /** * @private * Creates a menu item that displays the specified text. */ private function createMenuItem(text:String):Sprite { var item:Sprite = new Sprite(); item.graphics.beginFill(0x3399cc); item.graphics.drawRect(0, 0, 200, 30); item.graphics.endFill(); item.buttonMode = true; var textField:TextField = new TextField(); textField.autoSize = TextFieldAutoSize.LEFT; textField.defaultTextFormat = new TextFormat("Arial", 12, 0xffffff, true); textField.text = text; textField.x = 10; textField.y = (item.height - textField.height) / 2; textField.mouseEnabled = false; item.addChild(textField); return item; } //-------------------------------------- // Private Event Handlers //-------------------------------------- /** * @private * Move the menu item to the right over the course of 150ms. * Auto start the animation and clear all previous animations. */ private function menuItemRollOverHandler(event:MouseEvent):void { Animation.create(event.currentTarget, 150, {x: 40}, true, true); } /** * @private * Move the menu item back to the left over the course of 150ms. * Auto start the animation and clear all previous animations. */ private function menuItemRollOutHandler(event:MouseEvent):void { Animation.create(event.currentTarget, 150, {x: 10}, true, true); } /** * @private * Open a webpage when the menu item is clicked. */ private function menuItemClickHandler(event:MouseEvent):void { //based on the event's currentTarget (the menu item Sprite), //find the item's data using an Array filter. var filteredData:Array = this.menuItemData.filter(function(itemData:Object, index:int, source:Array):Boolean { return itemData.item == event.currentTarget; }); //if we successfully found the data, use navigateToURL to open the webpage. if(filteredData.length > 0) { var itemData:Object = filteredData[0]; navigateToURL(new URLRequest(itemData.url)); } } } }