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.
Related
I'm facing following problem from some times, and I always fail to overcome it.
I guess solution has to be provided in different way, but I would like to find out why this kind of code doesn't work.
Scenario:
Main class is Main.as in which there is a Sprite1 instance added to Main displayList.
There is also MyCustomClass instance which takes reference to Stage in constructor, to be able to dispatch an MyCustomClass.ABC event.
(in MyCustomClass)
stageRef.dispatchEvent(new Event(MyCustomClass.ABC));
then in my Main where sprite1 instance is placed on Main's displayList I want to listen for this MyCustomClass.ABC event dispatched by stage class reference from inside of MyCustomClass.
Finally if I put following code in my Main.as:
sprite1.addEventListener(MyCustomClass.ABC, onABC);
it doesn't work so I have to subscribe to this event directly by stage reference in Main class.
stage.addEventListener(MyCustomClass.ABC, onABC);
I thought that in the capture phase the event propagates from Stage to all the children, and sprite1 instance is a Main's child which is Stage's child. So, for me it should work but it doesn't.
I created an image describing objects on the displayList in this project. Please look at
link http://www.iv.pl/images/61170779800943350498.png
Thanks in advance for any explanations why it doesn't work. It doesn't work with any parameters I pass to listener (capturing) or dispatcher(bubbling).
capture phase event goes from target to stage while bubbling phase goes from stage to target. When stage is the target no object in the display list can catch that dispatched event since they cannot be included in the event phases. The target (stage) goes to stage then back to target (stage) and there's nothing in between to catch the event.
Typically when using stage to dispatch an event programmers want to make use of stage global nature (design that can be argued upon) but in that type of design it also mean that only stage should listen to its own dispatching.
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.
I'm working on a Flash project which is separated into separate scenes.
In Scene 1 I have multiple MovieClips (which include event listeners for RESIZE (and others) inside them).
In Scene 2 I have a few common MovieClips and new ones (which also include event listeners for RESIZE (and others) inside them).
After clicking a button from Scene 1 to go to Scene 2, it's fine, except for if I resize the stage and then I get the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
I know it's related to the event listeners, but it would be unrealistic to remove them each individually as it's expected there will be many.
If I undertand your situation correctly, I think you will in the end need to remove each listener individually, or add the resize listener only once. Since you mentioned scenes, am I right to assume you are working on the timeline? I also am assuming the null object reference error comes from a scene that has been removed from the stage, making reference to a display object that is no more, a ref to the stage after the scene has been removed, or just calling a function (the resize handler) on an object that no longer exists.
Some way to deal with this are:
Add some checking in the listener handler functions
if (!this.stage) return
To avoid the errors, but will not help if the object the function is a method of has been removed.
To avoid needing remember to remove hundreds of listeners, create removeAllListeners and addCustomEventListener functions. Instead of the usual addEventListener, call you addCustomEventListener which in turn will call addEventListener. Have addCustomListener store the target, listener function and event string in a dictionary or array of objects. removeAllListeners can loop through the dictionary or array and remove all your listeners. It is a bit like setting up an event hub, but does not go quite that far.
Instead of adding the RESIZE event listener to each scene, add it only once. Then in the listener function call a function on whichever scene is the active scene or view.
This last one is the approach I have seen most often, and is the most bullet proof. It may be tricky to implement on the time line, I have always been a little hazy on timeline variable scope.
Yes, so far as I know there is no good automated way to do this, however it would be a good practice to create a registerAllListeners and a removeAllListeners methods that manually add and remove the appropriate listeners to your object.
If the mouse is over an object before and while a MOUSE_OVER event is registered, does it trigger? I ask this because it appears that it doesn't in my program, and I want to know if this is a universal behavior of all MOUSE_OVER events. Is there a way around this?
I'm gonna avoid giving a code example here, because my program is large and complicated.
The MOUSE_OVER event will dispatch whenever the cursor enters the bounds of any interactive DisplayObject, such as a Sprite or MovieClip; this includes any of its children (see ROLL_OVER if you wish to ignore children).
As well, the event will dispatch in cases where an object is added to the stage and currently happens to be under the cursor.
It is important to make sure that your event listener has been registered before the Flash Player has dispatched the event -- system events are not queued beyond a single frame, and thus no handlers will be invoked for previous activity.
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!