Meet Andrew Nguyen

Flex/Flash Developer, Photographer, User Experience Expert

Andrew Nguyen On February - 18 - 2009

In ActionScript 3, Adobe got rid of attatching code directly to the MovieClip or Button you were working with on the the timeline.

So no more of this:
[cc lang="actionscript"]
on(press)
{
//do something
}
[/cc]

For code on the timeline that affects a button (whose instance name we’ll call “button”), we used to do this in AS2:
[cc lang="actionscript"]
button.onPress = function()
{
//do someting;
}
[/cc]

Now in AS3, we have something a little more “wordy”:
[cc lang="actionscript3"]
button.addEventListener(MouseEvent.MOUSE_DOWN,handleClick);
function handleClick(e:MouseEvent):void
{
//do someting;
}
[/cc]

One thing to understand about ActionScript 3 is that it is an event driven language. You listen for events, events get dispatched, and you handle them appropripately. I’ll break down the code for you.
[cc lang="actionscript3" first_line="1"]
button.addEventListener(MouseEvent.MOUSE_DOWN,handleClick);
[/cc]
What we’re saying on line 1 is here, we have a button that will listen for MouseEvent.MOUSE_DOWN events, and if that happens run the handleClick function.

[cc lang="actionscript3" first_line="2"]
function handleClick(e:MouseEvent):void
[/cc]
Here, we’re just defining the function “handleClick.”  It accepts an argument of type MouseEvent and :void means it doesn’t return anything.

As you can see, AS3 for buttons isn’t too difficult, just a little wordier.  Below, I’ve listed the old way of handling buttons with their new event listeners.  Just swap out the MouseEvents for what you need when you add the event listener.

AS2 AS3
button.onPress MouseEvent.MOUSE_DOWN
button.onRelease MouseEvent.MOUSE_UP
button.onRollOver MouseEvent.MOUSE_OVER
button.onRollOut MouseEvent.MOUSE_OUT
Categories: AS2 to AS3 Migration

10 Responses to " AS2 to AS3 Buttons "

  1. Tony says:

    Great Job, Ive been looking for something like this.

  2. brendan says:

    thanks alot. appreciated the thorough breakdowns.

  3. Dionys says:

    Guten Tag einen mega guten Blog, machst du Verdienste dadurch ? Ich selbst hab mit meinem Blog ewig nach Gewinnmöglichkeiten gesucht jedoch nix überzeugendes gefunden. Jetzt bin ich auf backlinkseller (hier der Link: http://bit.ly/bRl1Mi ) gestossen, dadurch bekomme ich schon fast 55 € im Monat. Hättest du vielleicht eine noch bessere Alternative fuer mich ? Waehre echt gut. 354h5j

  4. Wow this is a great resource.. I’m enjoying it.. good article

  5. Helmo says:

    guten Mittag wircklich tollen Webseitennamen, erhälst du Geld dadurch ? Ich selber habe für meiner Seite für lange Zeit nach Gewinnmöglichkeiten gesucht aber gar nichts gefunden. Jetzt bin ich auf backlinkseller (hier die URL: http://j.mp/bRl1Mi ) getroffen, dadurch bekomme ich schon fast 60 € im Monat. Hast du zufaellig eine zusätzliche Alternative fuer mich ? Waehre echt gut. 354h5j

  6. Valuable info. Lucky me I found your site by accident, I bookmarked it.

  7. Liza Rose says:

    Wow I am honestly the only comment to this amazing read?

  8. Давненько меня тут не было, респект за вашу работу.

  9. dxjidcrypv says:

    QLl5bZ jguoctqpobzd, [url=http://ugccotcxngnd.com/]ugccotcxngnd[/url], [link=http://shkprfxohtyb.com/]shkprfxohtyb[/link], http://fhfvzmmdjtlh.com/

  10. Emily says:

    thanks alot. appreciated the thorough breakdowns.

Leave a Reply

About Me

Andrew Nguyen is a User Experience developer and Photographer. If you’re looking for someone who cares about your customers, I’m your man.

Recent Comments