dropTarget (still) depends on mouse’s position, not just mc underneath

This is definitely old knowledge, but for those of us who have not yet stumbled upon everything that ActionScript and Flash can throw at us…

After pulling a few hairs out over a really simple drag and drop using the draggable object’s _dropTarget property, I just discovered this: the _dropTarget value depends not only on what movie clip might be under what you just dragged, it also takes the mouse position into account. Go on, say it: duh! The problem?

Say you’re working with a draggable slider, like a progress bar slider. If the the slider is taller than the progress bar below, you might grab it at the part that isn’t over the bar. Then when you release it, the _dropTarget property of the slider is an empty string, even if you’ve released it over the progress bar. That’s because the draggable object’s _dropTarget property will be the target you dropped in on only if the mouse is over that target too.

Flash 8, ActionScript 2… maybe ActionScript 3 behaves differently? (Like _dropTarget gets deprecated maybe? 😉 See below for update… )

It took a bunch of zooming in to discover why I was getting strange results. Sigh. Here’s a couple screenshots of a test file. ( Can’t embed flash files on a wordpress-hosted blog.) Anyway, the first image below shows the value you get if your draggable slider is released with the mouse over the _dropTarget you’re looking for: the string with the path to the target (named targetBar):

_dropTarget with mouse over target

The next screenshot shows that after you release your slider over the target, but your mouse isn’t over the target too, the _dropTarget is an empty string!

dropTarget with mouse not over target

[Update]

Just checked out what happens in ActionScript 3 with Sprite’s dropTarget property (using the example provided in the live docs for the Sprite class’s property dropTarget).

There’s still a problem when you drop the draggable object over the target, but the mouse isn’t over the target too. Instead of an empty string, though, dropTarget is apparently null. Using the example on live docs, we get an error where there’s this code:

trace(circle.dropTarget.name);

“TypeError: Error #1009: Cannot access a property or method of a null object reference.
at test_drop_target_fla::MainTimeline/mouseReleased()”

Long live hitTest().

Advertisements
This entry was posted in flash, actionscript, flex and tagged . Bookmark the permalink.

4 Responses to dropTarget (still) depends on mouse’s position, not just mc underneath

  1. Is there any way to get around it without having to use hitTest..

  2. Luke says:

    Yes, I actually just made a simple revuisoin ti the live docs code and fixed it:

    See, the error was generated here:

    function mouseReleased(event:MouseEvent):void {
    circle.stopDrag();
    trace(circle.dropTarget.name);
    }

    Because the stage doesn’t count as an object.. To fix this I just added an if statement check:

    function mouseReleased(event:MouseEvent):void {
    circle.stopDrag();
    if (circle.dropTarget != null)
    {
    trace(circle.dropTarget.name);
    }
    }

    This way it will only call upon the name property of dropTarget if there IS a dropTarget.

  3. Kuba says:

    On a related note…

    Do you guys know anything about hitTestObject?
    I am trying to write a function that will retrieve the name of an object that you are hovering over/dropping.

    Since dropTarget requires the mouse to be over the target object I thought that I could use hitTestObject or some sort of getBounds test. But I get into trouble because of the syntax of both of these functions.

    object.hitTestPoint(targetObject);

    As you can see this statement already requires me to know the name of the target, so it becomes useless when I want to find out that information.

    Any assistance would be appreciated.

  4. Lotus says:

    @Kuba (even if, after all this time…)
    I had the same problem but the possible items were in a list (array): so I looped through the list until I found the movieclip with hitTestObject (=true). If your items are onstage or in a container you can do the same (if are thousands, maybe it’s not the case…).

Comments are closed.