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?

CodeMouse92
  • 6,515
  • 12
  • 65
  • 124
  • 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()`](http://stackoverflow.com/questions/747709/actionscript-3-get-display-object-at-pixel) – Cameron Nov 23 '11 at 04:00

3 Answers3

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.

felipemaia
  • 2,841
  • 1
  • 14
  • 14
  • Your second option wouldn't break the dragging if the MouseEvent.MOUSE_UP listener was added to the stage. – Hawks Nov 23 '11 at 11:56
  • 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. – CodeMouse92 Dec 14 '11 at 06:08
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

Community
  • 1
  • 1
Shedokan
  • 1,062
  • 9
  • 20
  • getObjectsUnderPoint natively uses hitTestPoint to verify for all displays on the container. – felipemaia Nov 23 '11 at 14:37
  • 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? – Shedokan Nov 24 '11 at 21:54
  • 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. – felipemaia Nov 24 '11 at 21:58
  • 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! – CodeMouse92 Dec 14 '11 at 06:11
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.

merv
  • 42,696
  • 7
  • 122
  • 170
  • Why the -1? I'd like to know the situation in which this wouldn't work. – merv Nov 23 '11 at 16:24
  • 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) – CodeMouse92 Nov 23 '11 at 19:36
  • By the way, this only works on the Air runtime, which I am using. – CodeMouse92 Nov 23 '11 at 22:05
  • @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! – merv Nov 24 '11 at 00:00
  • Still, -1 should always include an explaination, imho. – CodeMouse92 Nov 24 '11 at 04:28
  • This is incompatible with Flash's traditional startDrag...apples and oranges. – CodeMouse92 Dec 14 '11 at 02:00