I found a great Flash blog yesterday when I was looking for source code to do Image masking and clipping.
This is the post that found very useful for a current project and decided to integrate his work into mine.
I have made some useful modifications to his code. His code was already excellent but not flexible enough for my needs. I needed a way to monitor the selected state of a button, so I added the needed functionality in both the MenuItem class and in the G94Menu class. I also added text formating to the button’s label so I could manipulate it during rollovers and clicks.
Now, after you click an item you can call the G94Menu.changeSelected and just pass the CLICK events current target at the MenuItem you want to select. I suppose this could be modified further to allow for multiple selections.
I will be modifying more of his work and integrating it with mine. I will post more updates at that time. Enjoy.
Here is the modified MenuItem Class:
/**
* ...
* @author Justin Windle [www.soulwire.co.uk]
* @version 0.1
*/
package com.soulwire.g94menu
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
public class MenuItem extends MovieClip
{
//- PRIVATE VARIABLES -------------------------------------------------------------------------------------
// Variable // Data Type
private var _id: int;
private var _txt: String;
private var _func: Function;
private var _params: Object;
private var _selected: Boolean;
//- PUBLIC VARIABLES --------------------------------------------------------------------------------------
public var _rtf:TextFormat;
public var _otf:TextFormat;
// Variable // Data Type
//- CONSTRUCTOR -------------------------------------------------------------------------------------------
public function MenuItem(txt:String, func:Function = null, params:Object = null, selected:Boolean = false)
{
_txt = txt;
_func = func;
_params = params;
_rtf = new TextFormat();
_otf = new TextFormat();
_selected = false;
}
//- PRIVATE METHODS ---------------------------------------------------------------------------------------
private function onRollOver(e:MouseEvent):void
{
bg.alpha = 0;
label.setTextFormat(_otf);
}
private function onRollOut(e:MouseEvent):void
{
bg.alpha = 1;
label.setTextFormat(_rtf);
}
//- PUBLIC METHODS ----------------------------------------------------------------------------------------
public function init(id:int, vis:Boolean):void
{
_id = id;
label.text = _txt;
label.defaultTextFormat = _rtf;
alpha = Number(vis);
_rtf.color = 0xFFFFFF;
_rtf.bold = true;
_otf.color = 0xCC0000;
_otf.bold = true;
if(_func != null)
{
buttonMode = true;
label.mouseEnabled = false;
addEventListener(MouseEvent.ROLL_OVER, onRollOver);
addEventListener(MouseEvent.ROLL_OUT, onRollOut);
addEventListener(MouseEvent.CLICK, _func);
onRollOut(null);
}
if( selected ){ selectItem() };
}
public function selectItem():void
{
selected = true;
removeEventListener(MouseEvent.ROLL_OVER, onRollOver);
removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
removeEventListener(MouseEvent.CLICK, _func);
bg.alpha = 0;
label.setTextFormat(_otf);
}
public function resetSelected():void
{
if(_func != null && selected)
{
buttonMode = true;
label.mouseEnabled = false;
addEventListener(MouseEvent.ROLL_OVER, onRollOver);
addEventListener(MouseEvent.ROLL_OUT, onRollOut);
addEventListener(MouseEvent.CLICK, _func);
onRollOut(null);
bg.alpha = 1;
label.setTextFormat(_rtf);
selected = false;
}
}
//- EVENT HANDLERS ----------------------------------------------------------------------------------------
//- GETTERS & SETTERS -------------------------------------------------------------------------------------
public function get id():int
{
return _id;
}
public function get params():Object
{
return _params;
}
public function get selected():Boolean
{
return _selected;
}
public function set selected( b:Boolean ):void
{
_selected = b;
}
//- HELPERS -----------------------------------------------------------------------------------------------
//- END CLASS ---------------------------------------------------------------------------------------------
}
}And here is the modified G94Menu class:
/**
* ...
* @author Justin Windle [www.soulwire.co.uk]
* @version 0.1
*/
package com.soulwire.g94menu
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.*;
import fl.transitions.easing.*;
public class G94Menu extends Sprite
{
//- PRIVATE VARIABLES -------------------------------------------------------------------------------------
// Variable // Data Type
private var _items: Array = new Array();
private var _visItems: int;
private var _spacing: int;
private var _offset: int;
private var _position: int;
private var _visibleheight: int;
//- PUBLIC VARIABLES --------------------------------------------------------------------------------------
// Variable // Data Type
//- CONSTRUCTOR -------------------------------------------------------------------------------------------
public function G94Menu(visItems:int, spacing:int, offset:int)
{
_position = 0;
_visItems = visItems;
_spacing = spacing;
_offset = offset;
_visibleheight = new int();
}
//- PRIVATE METHODS ---------------------------------------------------------------------------------------
private function sort():void
{
var _y:int;
var min:int = _position;
var max:int = _position + _visItems;
for(var i:int = 0; i < _items.length; i++) { var mc:MenuItem = _items[i]; var alph:int = Number(i >= min && i < max);
var yPos:int = mc.id * (mc.height + _spacing);
var yTar:int = _position * (_spacing + mc.height);
if(i < min) { _y = yPos - _offset - yTar; } else if (i >= max)
{
_y = yPos + _offset - yTar;
}
else
{
_y = yPos - yTar;
}
new Tween(mc, 'y', Regular.easeInOut, mc.y, _y, 8);
new Tween(mc, 'alpha', Regular.easeInOut, mc.alpha, alph, 8);
}
}
//- PUBLIC METHODS ----------------------------------------------------------------------------------------
public function addItem(item:MenuItem):void
{
item.y = (item.height + _spacing) * _items.length;
item.init(_items.length, _items.length >= _position && _items.length < _position + _visItems);
_visibleheight = ( item.height + _spacing ) * ( _visItems + 1 );
_items.push(item);
addChild(item);
sort();
}
/*
Get the currently selected MenuItem
*/
public function get selectedItem():MenuItem
{
for(var i:int = 0; i < _items.length; i++)
{
if( _items[i].selected )
{
return _items[i];
break;
}
}
return null;
}// public function get selectedItem():MenuItem
/*
DeSelect formerly selected items
*/
public function changeSelection( item:Object ):void
{
for(var i:int = 0; i < _items.length; i++)
{
if( _items[i] != item && _items[i].selected )
{
_items[i].resetSelected();
}
if( _items[i] == item && _items[i].selected == false )
{
_items[i].selectItem();
}
}
}// public function get selectedItem():MenuItem
public function scrollUp(e:MouseEvent = null):void
{
if(_position <= 0){return}; _position--; sort(); } public function scrollDown(e:MouseEvent = null):void { if(_position >= _items.length - _visItems){return};
_position++;
sort();
}
public function get visibleHeight():int { return _visibleheight; }
//- EVENT HANDLERS ----------------------------------------------------------------------------------------
//- GETTERS & SETTERS -------------------------------------------------------------------------------------
//- HELPERS -----------------------------------------------------------------------------------------------
//- END CLASS ---------------------------------------------------------------------------------------------
}
}


