0

When I listen to mouse click event on the stage, it seems it's not always responding to my mouse click event. what I have is:

stage.addEventListener(MouseEvent.CLICK, Test);

function Test(event:MouseEvent):void
{
    trace("test");
}

I usually have to click a few times randomly on the stage to get the trace statement. I thought when I add this event listener to the stage, it should respond to any mouse click within the swf area, no? Any ideas?

Thanks.

xxiaojun
  • 81
  • 1
  • 4
  • Something is wrong with your setup. This code works 100% in a fresh FLA. It's possible you might have a name conflict with the function. –  Feb 25 '11 at 17:50
  • try filling the stage with some color (but it should work anyway). and try to trace something before the function declaration to check your debugger – www0z0k Feb 25 '11 at 18:10
  • Are you clicking on anything that is on top of your stage? – Taurayi Feb 25 '11 at 18:15
  • it's 100% a working code: tested in an empty FlashDevelop project – www0z0k Feb 25 '11 at 18:17
  • @www0z0k sorry I meant the guy who asked the question. I think he is clicking on a display object lying on top of the stage, but then again I'm not sure if that stopw the event being dispatched from the stage. Also if he's using the timeline, maybe the code isn't executed until later on in the application. – Taurayi Feb 25 '11 at 18:47
  • Hi guys, thanks for the comments. There are movieclips completely covering the stage (like tiles). Does that prevent the event to be listened? The thing is sometimes it is working and I can get the trace, sometimes it's not. – xxiaojun Feb 27 '11 at 22:08
  • try adding a listener to `this` instead of `stage` – www0z0k Feb 28 '11 at 10:00

2 Answers2

0

Are you working with Flex? If so, try the following instead:

mx.core.Application.application.addEventListener(MouseEvent.CLICK, Test);

In Flex adding the listener to stage doesn't seem to work... no idea why.

sean
  • 2,560
  • 3
  • 18
  • 21
  • I don't know about the internals of Flex, but the only way an event can be stopped is with event.stopPropagation() or event.stopImmediatePropagation(). Try adding your stage click event with stage.addEventListener(MouseEvent.CLICK, onClick, true);. The last parameter is useCapture. See http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html – subb Feb 25 '11 at 18:27
  • pretty sure he's using flash cs3. – Taurayi Feb 25 '11 at 18:52
0

try a more "loosy" event

MouseEvent.MOUSE_DOWN

if you want to trigger your event only when user releases the button, try

MouseEvent.MOUSE_UP

kroe
  • 1,116
  • 3
  • 11
  • 23