AS3 Buttons
Thursday, December 6th, 2007It’s learning Actionscript 3 time for me so I thought I’d share a few things I’ve discovered lately.
First: Buttons.
Here is an example of a custom button. There is the built in SimpleButton class but sometimes you just need to roll your own.
This assumes you have a linked MovieClip in your Library with it’s class set to TestBtn.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class TestBtn extends MovieClip {
function TestBtn()
{
// UNLESS BUTTONMODE IS SPECIFIED AS
// TRUE, YOU'LL GET NO HANDCURSOR
this.buttonMode = true;
// IF YOU WANT TO HIDE THE HANDCURSOR
//this.useHandCursor = FALSE;
// STOPS THE EVENTS BUBBLING TO THE OTHER
// MOVIE CLIPS CONTAINED IN YOUR BUTTON
this.mouseChildren = false;
// ADD THE MOUSE 'ONCLICK' EVENT
this.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(event:MouseEvent):void
{
// TRACES THE NAME OF THE BUTTON CLICKED
trace(event.target.name, 'clicked');
}
}
}










