views:

32

answers:

3

I have buttons that have mouse_over, mouse_out and CLICK events. But when I click the button it takes me to another frame and the mouse_out event tried to fire. How do I stop that happening?

act1_btn.addEventListener(MouseEvent.CLICK, act1Pressed);
act1_btn.addEventListener(MouseEvent.MOUSE_OVER, act1Over); act1_btn.addEventListener(MouseEvent.MOUSE_OUT, act1Out); act1_btn.addEventListener(Event.ENTER_FRAME, act1EnterFrame);

function act1Over(e:MouseEvent):void { trace("over"); act1Animating = true; logo_1.visible = true; bubble.visible = true; txt1.visible = true; }

function act1Out(e:MouseEvent):void { act1Animating = false; logo_1.visible = false; bubble.visible = false; txt1.visible = false; }

function act1EnterFrame(e:Event):void { if (act1Animating && e.target.scaleY < 1.1) { e.target.scaleY += 0.02; e.target.scaleX += 0.02;

}

if (!act1Animating && e.target.scaleY > 1) { e.target.scaleY -= 0.02; e.target.scaleX -= 0.02; } }

function act1Pressed(e:MouseEvent):void { trace("clicked"); act1Animating = false; logo_1.visible = false; bubble.visible = false; txt1.visible = false; gotoAndStop(2); }

+2  A: 

Here are two ways to handle this:

1) Only assign the MOUSE_OUT listener in the MOUSE_OVER handler, then remove it after the MOUSE_OUT handler is done. I.e.,

function act1Over(e:MouseEvent):void {
  /* your code */
  act1_btn.addEventListener(MouseEvent.MOUSE_OUT, act1Out); 
}

function act1Out(e:MouseEvent):void {
  /* your code */
  act1_btn.removeEventListener(MouseEvent.MOUSE_OUT, act1Out); 
}

2) Use stopPropagation() in your CLICK handler:

function act1Pressed(e:MouseEvent):void {
  /* your code */
  e.stopPropagation();
}

Also, in the future, please use code tags to mark up your code!

Virgil Disgr4ce
thank you so much
James Cassimatis
A: 

When you click a button , you will trigger a MouseOver & a MouseOut event , if you don't wish to trigger the MouseOut event after the Click event , then you should remove the MouseOut event listener in the Click event listener.

This means that in order to be sure that you have a MouseOut listener when you MouseOver , you should add your MouseOut listener in the MouseOver listener.

Finally, you should remove the MouseOut event listener within the MouseOut listener.

PatrickS
thank you heaps
James Cassimatis
you're welcome ;)
PatrickS
A: 

It might not be a bad idea to give ROLL_OVER and ROLL_OUT MouseEvent a shot instead. These just fire once when someone rolls over the object, or rolls out, instead of firing continuously.

rectangular