Home > ActionScript 3.0 Tutorial, Flash Development > SimpleButton and Events in ActionScript 3.0 Tutorial

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.

Download the source files

  1. August 3rd, 2006 at 15:08 | #1

    Wicked, keep the samples coming!

  2. January 15th, 2007 at 17:15 | #2

    I liked this sample, clear and simple!
    Let’s keep the byte-size of our AS3 based application beyond our control!

  3. Luis Estrada
    June 7th, 2007 at 10:14 | #3

    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.

  4. Erik
    July 28th, 2007 at 07:13 | #4

    I have no idea what the “ActionScript 3.0 Settings Document class field” is. So the tutorial came to an abrupt stop there.

  5. July 30th, 2007 at 10:16 | #5

    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.

  6. Kevin
    September 7th, 2007 at 14:56 | #6

    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?

  7. Jesse
    November 12th, 2007 at 11:23 | #7

    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.

  8. Nykcu
    December 11th, 2007 at 07:57 | #8

    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.

  9. February 15th, 2008 at 13:05 | #9

    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!

  10. Durham Dave 7th
    June 29th, 2009 at 04:11 | #10

    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\ ?

  11. June 29th, 2009 at 07:53 | #11

    You create folders in Windows Explorer or Mac Finder.

  12. Ren
    September 9th, 2009 at 08:04 | #12

    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.

  13. October 22nd, 2009 at 15:15 | #13

    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.

  14. December 4th, 2009 at 02:55 | #14

    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!!

  15. Giovanni
    February 21st, 2010 at 23:47 | #15

    How can I add a link to the buttons?

  16. Laura
    March 31st, 2010 at 11:18 | #16

    I don’t see a way to position these buttons.

  1. No trackbacks yet.