AS3 Buttons

It’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');

		}
	}
}

One Response to “AS3 Buttons”

  1. Akbar Says:

    Hey… Thats really cool, error-free tutorial. Actually this is what I was looking for.

    There r plenty of tutorials available, most of them talks about creating a graphic inside as3 and almost all of them has some kind of error. This tut has really been a help.

    Keep it up..
    :-)

Leave a Reply