Joseph Webber
Joseph Webber

Reputation: 2173

Can I pass a button into its own MouseEvent function?

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);
}

Upvotes: 1

Views: 130

Answers (2)

Craig
Craig

Reputation: 814

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");
}

Upvotes: 0

Dave
Dave

Reputation: 169

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.

Upvotes: 1

Related Questions