views:

489

answers:

2

I have a special canvas class covering my entire stage, and a bunch of Sprite hotspots on the layer below. If I put the hotspots on the same layer as my canvas, the canvas does not register MOUSE_DOWN events if my mouse is inside one of the hotspots, since the hotspots are targeted instead. However, if I keep the hotspots on the lower layer, the canvas handles all the MouseEvents. Is there any way to make MouseEvents target more than one thing, so that both the canvas and hotspots respond to MouseEvents? (The canvas is a bitmap drawing class and the hotspots are supposed to trigger when the cursor touches them or clicks on them.)

+1  A: 

ActionScript has both event bubbling and event capturing. This means you can listen for events starting on containing elements or on children elements. You just need to create the right kind of event listener. Capturing starts catching events at the parent elements as it moves down the chain of the children until it reaches the element where the event was triggered. Event bubbling starts listening for events at the child and moves up to the parent.

The third argument to addEventListener, useCapture, determines if you want bubbling or capturing.

It sounds like you want event bubbling, so you want to set useCapture to false. Event listeners will be triggered on the child and on the parent.

Event Capturing and Bubbling

apphacker
Thank you! I fiddled with this a bit, but it seems like it still only allows one target per event. Is there any way to make a MouseEvent target a hotspot, run the function, and then continue down to the next layer? If I start drawing on one of the hotspots, the drawing canvas does not work.
Archagon
The target is the element or object that was clicked. There's only one target, and it's available in the event object. Sounds like you are still using event catching, and not bubbling. If you are using event bubbling it will do exactly what you want depending on the context.
apphacker
Ah! Got it. I was adding the hotspots as children of the stage, not the canvas. Thank you!
Archagon
+1  A: 

apphacker got it right.

There a two other things you might be interested:

the mouseChildren property and the getObjectsUnderPoint() method

good luck

George Profenza