I have two MovieClips (same parent) that overlap, both have a listener for mouse click.
But only the top most MC detects the click.
Is it possible to get both MCs detect the click using listeners ?
If not, is collisions a better way to do it, than using the getObjectsUnderPoint() ?
You can register the click event to a parent controller or class. When it receives the event callback it can then broadcast that back to all the other child MCs from that callback function. It's just a matter of managing the MCs - either keep a track of their names or add then all to an array so you can use a for loop to iterate through them.
Related
Is there any way by which I can make my button work, no matter in which scene or frame it is.
Is there any way by which i can call that Event Listener to another frame for that particular instance?
for example:
I have a button home_btn, I want this button to work in all scenes, without changing it's instance name. I already added an Event listener in first scene, but it doesn't work,
In another frame or scene.
Below is the code.
home_btn.addEventListener(MouseEvent.CLICK, process_it);
function process_it(event:MouseEvent):void
{
nextFrame();
}
I don't know how to use dispatch event function for my button.
// This only works for that particular frame.
Since you're doing this in the timeline, if you put the home_btn.addEventListener(MouseEvent.CLICK, process_it);
on the first frame of your main timeline, it will be registered even if you change to a different frame.
You can keep the function in its existing frame as that should still be called.
The reason why this works as opposed to the code being on a different frame is because frame 1 is where your timeline will start and process all the display items and code needed during this frame. That is any function, variable, or listener you want active will be running and/or stored into memory at the start of the application since it will always start on frame 1 until specified otherwise. If you have the listener on a different frame then you will have to gotoAndStop() to that frame to add the listener ( otherwise you are specifying that you don't want to add the listener until your application is at the frame ). Adding a listener to a display object on frame 1 is contingent upon the display object also being on that frame.
So basically, I have a large movieclip, lets call it "hit" and a bunch of smaller "thumb" movieclips below it. I have ROLL_OVER and ROLL_OUT event listeners on the main "hit" movieclip that I use to position the thumbs correctly (the component is a kind of ticker).
I am having a problem with getting the ROLL_OVER, ROLL_OUT, and CLICK event listeners to fire on the "thumb" movieclips that are below "hit".
Right now I am using a hit test, which kind of works, but I'd like a simpler way. I am an actionscript-3 newbie so any help would be appreciated. Thanks!
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/MouseEvent.html#!flash/display/InteractiveObject.html#event:rollOver
Dispatched when the user moves a pointing device over an InteractiveObject instance. The event target is the object under the pointing device or a parent of that object. The relatedObject is the object that was previously under the pointing device. The rollOver events are dispatched consecutively down the parent chain of the object, starting with the highest parent that is neither the root nor an ancestor of the relatedObject and ending with the object.
Object under hit won't dispatch ROLL_OVER event if it's not a parent of hit. Only objects on top will dispatch it. Mouse cursor must literally touch the object. If there is something between cursor and object, event won't be dispatched.
This thread appears to be asking the same question. The solution including getObjectsUnderPoint seems to be the best choice.
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)
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.
Is there a way to check the method that has been attached to the stage?
I have stage as global.. and need to fire some function in a object on mouseup...
Now it fires 2 or 3 depending how many objects i add..
I need something like..
if($.stage.hasEventListener(MouseEvent.MOUSE_UP, this.mouseUp) === false){
$.stage.addEventListener(MouseEvent.MOUSE_UP, this.mouseUp);
}
Or a better way to handle this?
I'm guessing you are adding the listener inside each object, no? That means every time you create an instance of your object you are adding yet another listener for stage mouse up events. If you really only want a single listener for this type of event, move it outside of the scope of the object and only add the listener once. Good luck!