Mouse over while dragging object in Flash CS5
Asked Answered
L

3

7

I have a project with Flash Professional CS5 and ActionScript 3.

I need to trigger an event when I drag an object over a particular spot, but haven't dropped it yet. Then, I need to trigger a different event when I leave that spot (still dragging). However, this should only occur while I am dragging on object.

The traditional mouse over and mouse leave events aren't working while dragging (only while not dragging).

How do I do this?

Lashawn answered 23/11, 2011 at 3:13 Comment(1)
I don't have time to answer, but the reason the events aren't firing is that there's another object in the way (the one being dragged), so the objects underneath don't get enter/leave events. There's a variety of ways around this, including playing with the dragged object's mouseChildren property (try setting it to false), or setting up a temporary listener for mouse move events on the stage in conjunction with getObjectsUnderPoint()Discommend
B
2

The reason it's not working is because the top DisplayObject (the one being dragged, is stealing the events for itself).

You have a few options, 1st is adding the MOUSE_MOVE event to the dragged object instead of the particular spot, and you could do a hitTestObject() to verify if they overlap, or a hitTestPoint() if the mouse is inside the particular spot.

So basically do this:

draggedObject.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);

function onMouseMove(evt : MouseEvent) : void {
    var particularSpot : MovieClip = MovieClip(evt.currentTarget.parent).getChildByName("particular spot object name");
    if(particularSpot.hitTestPoint(evt.mouseX, evt.mouseY)) // or use hitTestObject
    {
        // The mouse is on top of particular object
    }
    else
    {
        // The mouse is not on top of particular object
    }
}

Second one is to disable mouse events for the dragged object with mouseChildren and mouseEnabled properties, but that would break your current dragging, you would have to rearrange your events to the dragged object parent or the stage.

Box answered 23/11, 2011 at 4:13 Comment(2)
Your second option wouldn't break the dragging if the MouseEvent.MOUSE_UP listener was added to the stage.Tantra
Perfect! These are two very useful functions! Just as a note, the example code was far too overcomplicated. Accepted answer, with my working simplification in the edit.Lashawn
T
1

Simply call objectContainer.getObjectsUnderPoint(new Point(mouseX, mouseY)) and you'll get all objects under that point, and you can loop trough them and check if one of them is a `drop target.

See: Actionscript 3: get display object at pixel

Tolidine answered 23/11, 2011 at 10:24 Comment(4)
getObjectsUnderPoint natively uses hitTestPoint to verify for all displays on the container.Box
Really? I didn not know that, editing answer. But nevertheless it is probably a lot faster doing it in pure c++ rather than calling tens of function calls in an AS loop don't you think?Tolidine
Optimization is a tricky matter, I wouldn't say some obscure function is faster than some other obscure function without testing it before. But if he's only checking if one MovieClip is under the mouse, I don't see the need to find all of them.Box
This is good to know, even though it isn't the most efficient in this case. hitTestPoint works better for this purpose here, but I encourage other programmers to remember this function as well!Lashawn
C
0

If you happen to be building for the AIR runtime you could try using the nativeDragEnter event:

Dispatched by an InteractiveObject when a drag gesture enters its boundary.

and the nativeDragExit event:

Dispatched by an InteractiveObject when a drag gesture leaves its boundary.

Chickie answered 23/11, 2011 at 4:31 Comment(6)
Why the -1? I'd like to know the situation in which this wouldn't work.Chickie
Upvoting because, whether or not this answer is correct in my case, the docs back it up. Ergo, this answer is useful. (Please don't downvote unless "the answer is not useful", peoples)Lashawn
By the way, this only works on the Air runtime, which I am using.Lashawn
@JasonMc92 - You're totally right! I assumed because the events in the docs didn't have the AIR symbol next to them that they weren't AIR-only. Once I removed the AIR runtime from the filter, though, "POOF" - they were gone. I did think the "native" was a strange term, but figured it had been assimilated into normal FP, but kept the name. Guess that explains the -1!Chickie
Still, -1 should always include an explaination, imho.Lashawn
This is incompatible with Flash's traditional startDrag...apples and oranges.Lashawn

© 2022 - 2024 — McMap. All rights reserved.