SimpleButton and Events in ActionScript 3.0 Tutorial
In AS3, one of the most significant changes was to make the EventDispatch class the standard tool for calling events. This is a huge step in ActionScript as code written by different developers will become a lot more similar.
In the following example I will demonstrate how to use addEventListener to add a click event to a button that is built using the new SimpleButton object. The SimpleButton is a light weight alternative to the heavier MovieClip object.
package ca.flashdev.example {
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.events.Event;
public class ButtonTrace extends Sprite {
private var __button:SimpleButton = new SimpleButton();
public function ButtonTrace():void {
drawButton();
activateButton();
}
private function drawButton():void {
var down:Sprite = new Sprite();
down.graphics.lineStyle(1, 0x000000);
down.graphics.beginFill(0xFFCC00);
down.graphics.drawRect(10, 10, 100, 30);
var up:Sprite = new Sprite();
up.graphics.lineStyle(1, 0x000000);
up.graphics.beginFill(0x0099FF);
up.graphics.drawRect(10, 10, 100, 30);
var over:Sprite = new Sprite();
over.graphics.lineStyle(1, 0x000000);
over.graphics.beginFill(0x9966FF);
over.graphics.drawRect(10, 10, 100, 30);
__button.upState = up;
__button.overState = over;
__button.downState = down;
__button.useHandCursor = true;
__button.hitTestState = up;
addChild(__button);
}
private function activateButton():void {
__button.addEventListener(MouseEvent.CLICK, displayMessage);
}
private function displayMessage(e:Event):void {
trace("Don't push my buttons!");
}
}
}
Save this file as ButtonTrace.as in a folder structure of ca.flashdev.example.
Create a new movie and add ca.flashdev.example.ButtonTrace to the ActionScript 3.0 Settings Document class field. Save this file as button.fla in the same directory as your ca folder. Publish (Ctrl+Enter) your movie and you should see a button that you can click.
Wicked, keep the samples coming!
I liked this sample, clear and simple!
Let’s keep the byte-size of our AS3 based application beyond our control!
in here:
private function displayMessage(e:Event):void {
trace(“Don’t push my buttons!”);
}
shouldn’t the Event ‘e’ be a MouseEvent? As is e:MouseEvent.
I have no idea what the “ActionScript 3.0 Settings Document class field” is. So the tutorial came to an abrupt stop there.
Erik, you can find the “Document class:” field in the Properties panel. Right below the frame rate field. Also, make sure you are using Flash CS3 and not a previous version of Flash.
I just got CS3 and still weighing the pros and cons verse older actionscript. How come it seems to do simple click function it now takes like 30 lines of code compared to the old method which was about 3-4 lines?
from what I can tell from CS3 adobe has managed to totally destroy actionscript, taking something that was rather intuitive and easy, and making it overly complicated and confusing.
I totally agree with you Jesse, maybe this version is easy after you learn but 2.0 was much more intuitive and easy to use for beginners like me.
Jesse, Nykcu, I am howling now after trying for 5(!) FIVE hours to create a SimpleButton!!! Still no success until I am ready to spend 200 code strings.
Agree, they will kill AS and my job this time with AS3.0!
I don’t understand how to “Save this file as ButtonTrace.as in a folder structure of ca.flashdev.example.”
Do I create new folders in the Timeline and nest them?
Or do I create folders in Windows Explorer, C:\ca\flashdev\example\ ?
You create folders in Windows Explorer or Mac Finder.
So-called designers who can’t think outside the box need to change careers – like being a burger-flipper at McD’s.
“Application Designer” is a position that only exists because companies have been too cheap to hire real Application Developers.
Too bad for all you inflexible people who can’t think outside the box.
AS3 has been sorely needed for years – and now we’ll separate the real talent from the hacks.
Ouch! I agree that AS3 has been sorely needed for years and Flash Designers continue to tell me that it is extremely difficult to grasp. In my mind it takes both a good designer and a good developer to make a project great. Flash Designer’s should focus on what they do best, creating graphics and animating them. There should be very minimal or no code at all in their movies. The developer should take these assets and piece them together to create the end result. I think companies are going to have to consider hiring two specialized people instead of just one jack of all trades.
great tutorial, Fraser. And good responses to questions! I, for one, really need tutorials that break things down this far and are over before i ever start to kick and scream! I’ll be coming back for more for sure!!
How can I add a link to the buttons?
I don’t see a way to position these buttons.