From a push button to a hold button in AS3
Even wonder how to create a hold button in Flash? A typical flash simple button is a push button. What it means is the button responds to mouse click once even when the button is clicked and hold at the down state. To have the button to continuously executing while the button is in the down state, the following AS3 script is required:
package
{
import flash.display.SimpleButton;
import flash.display.MovieClip;
import flash.events.*;
public class Test extends MovieClip
{
public function Test()
{
hold_btn.addEventListener(MouseEvent.MOUSE_DOWN, doDown);
hold_btn.addEventListener(MouseEvent.MOUSE_UP, doUp);
}
public function doDown(e:MouseEvent):void{
addEventListener(Event.ENTER_FRAME, doEnter);
}
public function doEnter(e:Event):void
{
trace (”I’m in the downstate.”);
}
function doUp(e:MouseEvent):void
{
removeEventListener(Event.ENTER_FRAME, doEnter);
}
}
}

It’s a little more complicated than that. You would also want to know when the mouse is released outside the button, and most probably if the mouse leaves the stage.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class HoldButton extends Sprite
{
private var button : Sprite;
public function HoldButton()
{
super();
button = new Sprite();
button.graphics.beginFill( 0xFF0000, 1 );
button.graphics.drawRect( 0, 0, 50, 50 );
button.graphics.endFill();
button.addEventListener( MouseEvent.MOUSE_DOWN, onButtonMouseDown );
addChild( button );
}
private function onButtonMouseDown( event:MouseEvent ):void
{
addStageListeners()
}
private function onStageMouseUp( event:MouseEvent ):void
{
removeStageListeners()
}
private function onStageMouseLeave( event:MouseEvent ):void
{
removeStageListeners()
}
private function onStageEnterFrame( event:Event ):void
{
trace( “onStageEnterFrame” );
}
private function addStageListeners():void
{
stage.addEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
stage.addEventListener( Event.MOUSE_LEAVE, onStageMouseLeave );
stage.addEventListener( Event.ENTER_FRAME, onStageEnterFrame );
}
private function removeStageListeners():void
{
stage.removeEventListener( MouseEvent.MOUSE_UP, onStageMouseUp );
stage.removeEventListener( Event.MOUSE_LEAVE, onStageMouseLeave );
stage.removeEventListener( Event.ENTER_FRAME, onStageEnterFrame );
}
}
}
Brian…this worked perfect for what I needed. Thanks!