PDA

View Full Version : Event for stage but not element


rhille
11-07-2008, 01:59 AM
Hello again

I am now looking for a way to check when the user clicks outside a box. For example, if I have a message on the screen and the user clicks away from that box, I would like to close the message. Any way to do that?

Thanks


Rick

aamenabar
11-07-2008, 02:50 AM
I cases similar to that, what I did, is place a MovieClip (back_mc) with alpha=0 behind the box, and then add a Listener for CLICK events on back_mc, and when clicked, just remove the back_mc and remove the listener...

Do I make sense??

fx.barrett
11-07-2008, 03:00 AM
You could do something like this:

// button_mc is a movie clip on the stage
stage.addEventListener(MouseEvent.CLICK, onStageClick);
button_mc.addEventListener(MouseEvent.CLICK, onButtonClick);

function onStageClick(event:MouseEvent):void
{
if (event.eventPhase == EventPhase.AT_TARGET)
trace("Stage has been clicked.");
}

function onButtonClick(event:MouseEvent):void
{
trace("Button has been clicked.");
}
This way, you can check when the user clicks the button or the stage around it... you can apply this idea once your message box is already open ( to check if the users clicks yourBox_mc or the stage ), once you close it you can remove the 2 event listeners.

Mazoonist
11-07-2008, 03:08 AM
You can also add an event listener to the stage to listen for your mouse click. Then, in the handler function, compare event.target to your box's instance name (or variable name) to see if it was the item clicked on:
stage.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:MouseEvent):void {
if(event.target != box) {
trace("stage or some other object clicked, but not box");
}
}
I see fx.barrett has posted while I was composing this, and his answer is very similar. Heh.

rhille
11-07-2008, 04:26 AM
Thank you for your answers. I tried the suggestions made by fx.barrett and Mazoonist but I get the same error message in both cases: 1120: Access of undefined property button_mc.

What am I missing...


Rick