h1

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

November 20, 2007

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().

One comment

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



Leave a Comment