Skip to main content
Inspiring
May 29, 2007
Question

CS3 Event Handlers

  • May 29, 2007
  • 4 replies
  • 517 views
I have a MovieClip that I am using as a button (which was previously possible in Flash 8). I created the up, over, and down frames in the button and am writing the AS3 code for the button. It looks like this:

import flash.events.MouseEvent;

function playMovie(event:MouseEvent):void
{
clip1.play();
}
function overState(event:MouseEvent):void {
playButton.gotoAndStop(5);
}
function downState(event:MouseEvent):void {
playButton.gotoAndStop(10);
}
function outState(event:MouseEvent):void {
playButton.gotoAndStop(1);
}

playButton.addEventListener(MouseEvent.CLICK, playMovie);
playButton.addEventListener(MouseEvent.MOUSE_OVER, overState);
playButton.addEventListener(MouseEvent.MOUSE_DOWN, downState);
playButton.addEventListener(MouseEvent.MOUSE_OUT, outState);

That's an awful lot of code for 1 button. If I had 5 or 6 buttons, that's a lot to edit. Is there an easier way to do this?
This topic has been closed for replies.

4 replies

kglad
Community Expert
Community Expert
May 30, 2007
the mouse handlers are mutlipurpose (or general) with each (except playMovie) being the same for all the buttons.

you still need a different MOUSE_DOWN handler for each button or a switch of some kind in one handler.

but that's no different than as 1 and as 2.

and now that i look at it, it's actually less typing to code in as3 than the typical coding used in as2 for multiple buttons in a for-loop.
Inspiring
May 29, 2007
Arrg, foiled by the superquick reply of kglad! hehe


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
May 29, 2007
padrepio,

> I have a MovieClip that I am using as a button (which was
> previously possible in Flash 8).

It's still possible in Flash CS3, whether you're using ActionScript 2.0
or 3.0. Depending on the complexity of your movie, you might just want to
set the Publish Settings for AS2. Maybe?

> I created the up, over, and down frames in the button and
> am writing the AS3 code for the button. It looks like this:
>
> import flash.events.MouseEvent;

If you're writing this code in a timeline keyframe, you can omit this
import statement. That helps a bit. ;)

> function playMovie(event:MouseEvent):void
> {
> clip1.play();
> }
> function overState(event:MouseEvent):void {
> playButton.gotoAndStop(5);
> }
> // etc.

> playButton.addEventListener(MouseEvent.CLICK, playMovie);
> playButton.addEventListener(MouseEvent.MOUSE_OVER, overState);
> // etc.

> That's an awful lot of code for 1 button.

Well, yes and no. I remember when AS2 was brand new, and instead of
something like this:

on (rollOver) {
// do something
}

... it suddenly became this ...

btnInstanceName.onRollOver = function():Void {
// do something
}

... which did look like a whole lot more, I remember. To make the
formatting analogous, that could have also been done with a named function
...

function customFunction():Void {
// do something
}
btnInstanceName.onRollOver = customFunction;

... which is even longer -- but that does enter the ballpark of the
named-function approach you're using in AS3. Looking at it in that way, the
AS3 version isn't excessively more ... just a tad more.

> If I had 5 or 6 buttons, that's a lot to edit. Is there an easier way to
> do this?

Well, you could use a for() loop and cycle among your buttons.

var btns:Array = new Array(btn1, btn2, btn3);

for (var i:Number = 0; i < btns.length; i++) {
btns .addEventListener(MouseEvent.CLICK, playMovie);
btns
.addEventListener(MouseEvent.MOUSE_OVER, overState);
btns .addEventListener(MouseEvent.MOUSE_DOWN, downState);
btns
.addEventListener(MouseEvent.MOUSE_OUT, outState);
}

Of course, your handler functions when then have to check which button
had been clicked in order to know what to do, but that can be accomplished
by checking the incoming Event object those functions receive as a
parameter.

function playMovie(e:Event):void {
trace(e.target);
trace(e.target.name);
}


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


kglad
Community Expert
Community Expert
May 29, 2007
you can't do it much easier for 1 button. but coding for 100 buttons doesn't require much more coding.

just use a for-loop to loop assign your addEventListeners and use event.currentTarget in your handler functions instead of playButton. if your buttons don't have conveniently chosen references the references will have to be added to an array so you can use that in your for-loop.
padrepioAuthor
Inspiring
May 30, 2007
Would I need a for loop for every button? Where does event.currentTarget go?
kglad
Community Expert
Community Expert
May 30, 2007
: