1

I have multiple MovieClip Symbols published with Flash into FlashDevelop (I'll only use 2 in my example). Each have 3 frames for default, hover and click that I'm using as buttons.

private var btnPlay:PlayButton, btnQuit:QuitButton;

btnPlay = new PlayButton();
btnQuit = new QuitButton();

btnPlay.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
btnPlay.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
btnPlay.addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler);

btnPlay.buttonMode = true;
btnPlay.useHandCursor = true;

function onRollOverHandler(myEvent:MouseEvent):void {
    btnPlay.gotoAndStop(2);
}

function onRollOutHandler(myEvent:MouseEvent):void {
    btnPlay.gotoAndStop(1);
}

function onPressHandler(myEvent:MouseEvent):void {
    btnPlay.gotoAndStop(3);
}

function onReleaseHandler(myEvent:MouseEvent):void {
    btnPlay.gotoAndStop(2);
}

// Same code for btnQuit here, but replace btnPlay with btnQuit

Instead of adding new EventListeners to every button that do practically the same thing like what I'm doing above, is there a way I could just pass in the button itself to the MouseEvent functions something like this? (I realize this might be difficult since all buttons are their own class)

btnPlay.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler(btnPlay));
btnPlay.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler(btnPlay));
btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler(btnPlay));
btnPlay.addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler(btnPlay));

function onRollOverHandler(myEvent:MouseEvent, inButton:MovieClip):void {
    inButton.gotoAndStop(2);
}

function onRollOutHandler(myEvent:MouseEvent, inButton:MovieClip):void {
    inButton.gotoAndStop(1);
}

function onPressHandler(myEvent:MouseEvent, inButton:MovieClip):void {
    inButton.gotoAndStop(3);
}

function onReleaseHandler(myEvent:MouseEvent, inButton:MovieClip):void {
    inButton.gotoAndStop(2);
}
Joseph Webber
  • 2,010
  • 1
  • 19
  • 38

2 Answers2

1

Maybe I am misunderstanding, but "event.target" provides you a reference to the button that has been clicked. So if you want to do something to the clicked button, you would write:

myEvent.target.gotoAndStop(1);

Or sometimes you might need to use "currentTarget". You'd still need to create listeners for each function but could use one set of handlers.

Dave
  • 169
  • 3
  • 7
  • In your 'one set of handlers' how would you pass the frame number to stop on, without re-writing the ’MouseEvent' class? – Craig Mar 22 '14 at 18:10
  • Oh. I was hoping you were using a standard movie clip for showing the buttons so the frame numbers would be a constant. However, you can still use this method if you attach classes to the buttons and then store the frame numbers as properties of the buttons. So create a class like BasicButton and attach it to your quit and play buttons. Then create properties on BasicButton through public variables or getters and setters such as a "overFrameNum" number variable. Your event handler then should contain: "myEvent.target.gotoAndStop(myEvent.target.overFrameNum); – Dave Mar 22 '14 at 18:19
  • I'm not quite following that. In the OP's problem the gotoAndStop() frame numbers vary with the event type, not with the button (of which there is only 1). So how would tell your listener function which frame to stop on? – Craig Mar 22 '14 at 18:36
  • Couldn't you use the constants then as per the original answer so you'd have "myEvent.target.gotoAndStop(1)" for out, "myEvent.target.gotoAndStop(2)" for over? Or you could store the frames on the buttons as I said and just assign the same values, so you'd have "btnPlay. overFrameNum=2" and "btnQuit. overFrameNum=2" – Dave Mar 22 '14 at 19:52
  • I thought about this for a bit. Please see my edit in my answer. Thanks. – Craig Mar 23 '14 at 05:59
  • That's cool. I've never used type. Of course, the ultimate best thing to do would be to create a button class, attach it to the play and quit buttons, put all the code in there. Then, if you have 1 or 100 buttons its all the same. – Dave Mar 23 '14 at 09:29
  • @Dave Thanks a bunch, this actually works! When I wrote "myEvent." nothing came up in terms of methods or properties that I could use, so I assumed there were none. The only problem I had was when I called `removeChild(myEvent.target);` in the onClick() function, I got the error: `Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject`. To fix that I just typecasted myEvent.target to a DisplayObject and bingo! – Joseph Webber Mar 24 '14 at 04:33
0

Simple answer: No. You could go to some trouble to override the MouseEvent class and allow it to send additional parameters, but why bother in this case? You don't seem to be saving any code.

SLIGHT UPDATE:

Here's a possibly-useful simplification of your original code. It saves a few lines-of-code and uses just a single handler function. Obviously, the 'trace' statements could be replaced by various 'gotoAndStop()' statements:

btnPlay.addEventListener(MouseEvent.ROLL_OVER, HandleAll);
btnPlay.addEventListener(MouseEvent.ROLL_OUT, HandleAll);
btnPlay.addEventListener(MouseEvent.MOUSE_DOWN, HandleAll);
btnPlay.addEventListener(MouseEvent.MOUSE_UP, HandleAll);

function HandleAll(e)
{
    if (e.type == "rollOver")  trace("rollover");
    if (e.type == "rollOut")   trace("rollout");
    if (e.type == "mouseDown") trace("mousedown");
    if (e.type == "mouseUp")   trace("mouseup");
}
Craig
  • 814
  • 1
  • 6
  • 9
  • Okay, thanks. Looks like it's custom MouseEvent functions for every button then. – Joseph Webber Mar 22 '14 at 05:34
  • Well, actually it's STANDARD listener functions. But that's o.k. – Craig Mar 22 '14 at 05:38
  • I marked your reply "No" as the answer, but then Dave replied with the actual answer I was looking for. Also, your updated method wouldn't allow me to target the button that called the event, which was a must. – Joseph Webber Mar 24 '14 at 09:24