Here’s a straightforward ActionScript 3.0 BasicButton class that I find pretty versatile for a number of unique button situations. In the future, I will post a slightly more advanced version of this class. To implement this class, set the base class of a movie clip in the library to point to the class location:

…and follow the timeline structure as follows (the “hit_mc” layer allows a hit sprite with an instance name “hit_mc” to be used as the button’s hit area):

It’s a good starting point for almost any button situation. The flexibility of the timeline structure allows for complex animations and it can be extended to create more specific types of buttons, i.e. ToggleButton.
[cc lang="actionscript" tab_size="2" lines="100"]
package com.jasonwoan.display {
/**
* [BasicButton]
*
* @class BasicButton
* @author Jason Woan
* @version 1.0.0
* @date March 3, 2008
* @langversion ActionScript 3.0
* @playerversion Flash Player 9
*
* @history 1.0.0 (March 3, 2008) Created initial version of BasicButton for AS3.
*/
//————————————————–
// IMPORT STATEMENTS
//————————————————–
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.*;
public class BasicButton extends MovieClip {
//————————————————–
// PUBLIC STATIC PROPERTIES
//————————————————–
/**
* The fully qualifed class name and path. This can be instanceed from the class but the value
* can also be retrieved from the instance using the getter fullyQualifedClassPath
*/
public static var FULLY_QUALIFIED_CLASS_PATH:String = “com.jasonwoan.display.BasicButton”;
//————————————————–
// PRIVATE PROPERTIES
//————————————————–
/**
* A reference to this instance.
*/
protected var _instance:BasicButton;
/**
* A boolean flag indicating whether the button is on.
*/
private var _is_on:Boolean = true;
/**
* An integer id representing this button
*/
private var _id:uint;
/**
* A boolean flag indicating that the button is selected
*/
private var _is_selected:Boolean = false;
//————————————————–
// CONSTRUCTOR
//————————————————–
/**
* Constructor
*/
public function BasicButton()
{
_instance = this;
init();
}
//————————————————–
// GETTERS / SETTERS
//————————————————–
/**
* Returns the fully qualified class path which includes the class package and class name as a string
* @return - String representing the full class path.
* For example: com.jasonwoan.display.BasicButton
*/
public function get fullyQualifiedClassPath():String
{
return BasicButton.FULLY_QUALIFIED_CLASS_PATH;
}
/**
* Returns the button’s on state.
* @return
*/
public function get on():Boolean
{
return _is_on;
}
/**
* Sets button’s on state.
* @param p_param (Boolean)
* @return void
*/
public function set on(p_param:Boolean):void
{
_is_on = p_param;
if (_is_on) {
_instance.mouseEnabled = true;
_instance.gotoAndStop(”up”);
} else {
_instance.mouseEnabled = false;
_instance.gotoAndStop(”off”);
}
}
/**
* Returns the button’s id.
* @return
*/
public function get id():uint
{
return _id;
}
/**
* Sets the button’s id.
* @param p_param (uint)
* @return void
*/
public function set id(p_param:uint):void
{
_id = p_param;
}
/**
* Returns the button’s selected state.
* @return
*/
public function get selected():Boolean
{
return _is_selected;
}
/**
* Sets the button’s selected state.
* @param
* @return void
*/
public function set selected(p_param:Boolean):void
{
_is_selected = p_param;
if (_is_selected) {
_instance.gotoAndStop(”over_hold”);
_instance.mouseEnabled = false;
} else {
_instance.gotoAndStop(”up”);
_instance.mouseEnabled = true;
}
}
//————————————————–
// EVENT HANDLERS
//————————————————–
/**
* Invoked when the button is rolled over.
* @param p_event (MouseEvent)
* @return void
*/
private function onButtonRollOver(p_event:MouseEvent):void
{
if (_is_on) {
_instance.gotoAndPlay(”over”);
} else {
_instance.gotoAndStop(”off”);
}
}
/**
* Invoked when the button is rolled out.
* @param p_event (MouseEvent)
* @return void
*/
private function onButtonRollOut(p_event:MouseEvent):void
{
if (_is_on) {
_instance.gotoAndPlay(”out”);
} else {
_instance.gotoAndStop(”off”);
}
}
//————————————————–
// PUBLIC METHODS
//————————————————–
/**
* Destroys this object for garbage collection.
* @return void
*/
public function destroy():void
{
destroyListeners();
_instance = null;
}
//————————————————–
// PRIVATE METHODS
//————————————————–
/**
* Initializes class instance.
* @return void
*/
private function init():void
{
createListeners();
_instance.buttonMode = true;
_instance.mouseChildren = false;
_instance.on = true;
// check to see if hit area mc exists, if so assign it as the hit area
var hit:Sprite = _instance.getChildByName(”hit_mc”) as Sprite;
if (hit) {
_instance.hitArea = hit;
}
}
/**
* Creates event listeners for the button.
* @return void
*/
private function createListeners():void
{
_instance.addEventListener(MouseEvent.ROLL_OVER, onButtonRollOver);
_instance.addEventListener(MouseEvent.ROLL_OUT, onButtonRollOut);
_instance.addEventListener(MouseEvent.MOUSE_UP, onButtonMouseUp);
}
/**
* Removes event listeners for the button.
* @return void
*/
private function destroyListeners():void
{
_instance.removeEventListener(MouseEvent.ROLL_OVER, onButtonRollOver);
_instance.removeEventListener(MouseEvent.ROLL_OUT, onButtonRollOut);
_instance.removeEventListener(MouseEvent.MOUSE_UP, onButtonMouseUp);
}
}
}
[/cc]
