/* Class ButtonClass created by Alon Farchy. This class is the superclass of all things clickable. I wish flash supported abstract classes. There are two ways a button can be created: By code and by setting this class as the base class of a MovieClip in the flash library. If done by code, call drawBtn(btnSkin:MovieClip):void to provide graphics for button and set changeArea to area of button that changes. Either way, the supplied MovieClip must have frames labeled "up","down","over", and "out". */ package code.buttons { import flash.display.MovieClip; import flash.events.MouseEvent; import flash.events.Event; import code.IFade; public class ButtonClass extends MovieClip implements IFade { //vars protected var changeArea:MovieClip; protected var btnState:String; protected var _destroy:Boolean = false; //for implementing IFade private var fadeStep:Boolean; public var fadeRate:Number = .05; //const. public function ButtonClass() { stop(); btnState = "up"; setButtonListeners(); } //Event Listeners protected function setButtonListeners() { addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler); addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler); addEventListener(MouseEvent.CLICK, mouseClickHandler); } //Draw btn - Adds a code-created button (btnSkin) as a child of this object. protected function drawBtn(btnSkin:MovieClip) { btnSkin.gotoAndStop("up"); btnSkin.buttonMode = true; addChild(btnSkin); } //Changes the state of this button. If the button was created by code, changeArea should be set to the MovieClip //object that changes states when clicked / moused over / etc. protected function changeState( s:String ):void { btnState = s; if(changeArea != null) changeArea.gotoAndStop(btnState); else gotoAndStop(btnState); } //Implementing IFade ************************************************** public function fadeIn():void { fadeStep = true; addEventListener(Event.ENTER_FRAME, fadeHandler); } public function fadeOut(destroy:Boolean = false):void { fadeStep = false; addEventListener(Event.ENTER_FRAME, fadeHandler); _destroy = destroy; } public function fadeToggle():void { addEventListener(Event.ENTER_FRAME, fadeHandler); } //EVENT HANDLERS ***************************************************************** //Mouse Events protected function mouseOverHandler(event:MouseEvent) { changeState("over"); } protected function mouseOutHandler(event:MouseEvent) { changeState("up"); } protected function mouseDownHandler(event:MouseEvent) { changeState("down"); } protected function mouseClickHandler(event:MouseEvent) { changeState("over"); } //IFade Handler protected function fadeHandler(event:Event):void { switch(fadeStep) { case true: this.alpha += fadeRate; if(this.alpha >= 1) { fadeStep = false; removeEventListener(Event.ENTER_FRAME, fadeHandler); } break; case false: this.alpha -= fadeRate; if(this.alpha <= 0) { if(_destroy && parent != null && parent.getChildByName(this.name) != null) { _destroy = false; parent.removeChild(this); } fadeStep = true; removeEventListener(Event.ENTER_FRAME, fadeHandler); } break; } } } }