AS3 - Detect if mouse is down, even if overlaps - actionscript-3

I would like to know if the mouse button is down, even if another object is being clicked. How do I do that? Simply adding event listeners doesn't work as it does not trigger if something else is on top of the object.
Thanks

Add both a Click & MouseDown event listeners to the stage.
If an object is clicked , the event will bubble up to the stage so you should be able to register it and react accordingly.
You can also check the event.currentTarget property to find out where the event originated from , this should tell you if it was the stage or an object being clicked as well as where the mouse down event came from.

Add a mouse down event listener to the stage (or other parent of all objects in question)

Related

How to check or print which element handle specific event

happy new year 2017!!
I want check which elememt is owner of event.
for example, basically mouse wheel event handler(owner) is body element.
if I had several vertical scrollbar, mouse wheel event handler will be changed.
please tell me how to print event owner(especially mouse wheel event).
Thanks!
The event Object has several properties. You are looking for currentTarget or target, depending on how you registered the event handler and which element actually called it.
You can play with them to see.

Mouse Click vs Enter Frame Stage reference

I have a document class with some constructor code. I have two event listeners in the constructor. One of them is an enter frame listener and the other is a mouse click listener. The enter frame function moves a rectangle on the stage. The mouse event function removes it from the stage when its been clicked.
When I used the following:
addEventListener(MouseEvent.CLICK, checkTarget);
The rectangle didn't get removed. But when I used:
stage.addEventListener(MouseEvent.CLICK, checkTarget);
It worked fine. I kept the enter frame without referencing the stage and it worked.
addEventListener(Event.ENTER_FRAME,update);
Why did the mouse click need the reference to the stage and the enter frame didn't? Aren't they both added to the same thing at the same time?
mouse event needs something to work with but ENTER_FRAME is built to work with stage.
I would change the mouse event listener to listen to when you clicked at a mc just like that rectangle if I where you.

Actionscript track onmouseup outside stage, possible?

So I'm working on a button that plays a sound when clicked or 'mousedown' on, and stops playing when releasing the mousebutton. The mouseup event is set on the stage object.
So the issue is when the cursor is dragged outside the flash movie and then released, the sound doesn't stop since the onmouseup doesn't register anymore.
Thus is there a way to detect either mouseup events, or mouse coordinates outside the stage/flash object itself with actionscript?
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#event:mouseLeave
Add an Event.MOUSE_LEAVE listener to the Stage. This event fires in 2 ways:
Mouse button is already released and leaves the Stage
Mouse button is down and leaves the Stage, then mouse button is released
If your mouse button is down and you leave the Stage, it doesn't fire. An example reason is this: You start dragging a MovieClip and you go off Stage, when you come back to the Stage naturally you expect to still be dragging.

As3 - Child heredit event listener. How to stop?

I'm an as3 newbie. I'm experiencing this strange problem. I've created a button (type: MyButton) with two child, a text (TextField) and a image icon (type: MyIcon).
Then I've append an eventlistener mouse_click on my button.
As soon as I click on the text, the e.target on the handeler function is recognized of MyButton type. Otherwise, if I click on the image icon (child of button) the e.target is MyIcon type, instead of MyButton.
How can I prevent this? I need all click to be recorded on the button, where I've stored some attributes I need on handeler function.
Thanks.
Use the e.currentTarget instead. It returns the object that the MouseEvent has currently bubbled to. e.target returns the object that the MouseEvent actually started on.
Read about Event bubbling here. It's a very important concept to grasp.

Listen to click event on overlapped sprites

I have two sprites in my movieclip, one under the other, and i want both to listen to mouse clicks event.
I found that only the top level sprite receives the event when i click on it.
I need to dispatch the events on both, so I can't use mouseenabled=false.
Is there a simple workaround for this?
Another solution is, that you listen to the click-event on the upper sprite, and fire it again with
dispatchEvent(new MousEvent(event));
Have a look at the "bubble" property of the event.
You can use the 'getObjectsUnderPoint' method native to the DisplayObjectContainer
You can see how it works here:
http://snipplr.com/view/34945/as3-trace-movieclips-under-mouse/
and the AS3 reference here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html
You would listen on the stage for any click event and if one occurs, check to see if the element you require is under that click position.