Catch MouseOver Event When Dragging Another Object

This is not a difficult thing, but it caused some consternation in a recent project because we were trying to perform this simple task in the depths of what was a more complex piece of code.

So, what we needed to do what catch the MouseOver on DisplayObjects while dragging another DisplayObject. Naturally since the object being dragged under the mouse has the focus, nothing below that DisplayObject is receiving those mouse events. And in pure AS3 there is no DragEvent.DRAG_OVER as you would have available in Flex.

[Note to Self: you tried this solution initially, remember? And you dropped it because of a complication with targeting when the item was dropped over multiple target objects. Just because something doesn’t work initially, take a breath and remind yourself it shouldn’t and probably isn’t as hard as your supercharged, hyper-caffeinated brain is making it out to be. Keep it simple and you may already have the right solution. Dude.]

To get the mouse events to lower elements we need to enable mouseChildren. This is great, but dropping the object becomes problematic at times. The trick that we all had to learn when jumping from AS2 to AS3 is to not only catch the MouseEvent.Mouse_UP from the object but ALSO from the stage. This ensures we always know when the user releases their mouse, even if they are outside the Flash area when that occurs.

After trying some crazy getObjectsUnderPoint() options, we came back to the original solution with fresh eyes and it turns out that we had the answer all along.

If you aren’t already doing this, I recommend creating a small project and testing out a solution apart from the existing codebase. It could be that the architecture and complexity of the project is clouding your vision from seeing the obvious answer and giving you the confidence to forge ahead.

Project code: DragOver.zip

My simple test for your viewing pleasure:


Code after the break…

» continue reading
Categories: FlashGeneralPermalink