Creating Drag-and-Drop Targets
Drag-and-drop gets a lot more meaningful when users have a target on which to drop the dragged objects. Whether you're building an online shopping cart or creating learning media, targets provide a way for users to trigger events by manipulating onscreen objects so that they interact with each other.In this task, you'll create drop targets for the monsters. In addition, you'll set up a test to determine whether a user dropped the monster on the correct target. Along the way, you'll learn two new actions: you'll use the hitTest() action to determine whether a monster was released over its target, and you'll use the trace() action to make Macromedia Flash generate feedback to make sure that hitTest() actually worked. After you verify that drag-and-drop works, you'll remove the TRace() action and add real code to create the desired behaviors.
1. | Select Frame 10 of the monster targets layer in the timeline, and drag an instance of monsterTarget from the Library onto the stage. Use the Property inspector to give it an instance name of chiron_targ_mc . Use the Property inspector to set its X value to 118 and its Y value to 76 to position the instance over the river of blood, to the left of the Violence label . |
The monsterTarget movie clip is simply a white rectangle with a black border large enough to enclose each of the monsters. This particular instance is the target for Chiron, hence the instance name chiron_targ_mc. The instance name enables you to write the ActionScript that can compare the _x and _y position attributes of the two objects (the chiron_mc drag object and the chiron_targ_mc target). If these two sets of position coordinates are the same (or close), then the ActionScript knows that Chiron was dropped over its target.[View full size image]


3. | Press Ctrl+Enter (Windows) or Command+Return (Macintosh) to test the movie. Drag Chiron around, letting him go periodicallysometimes over the target, sometimes away from it . |
As you release Chiron in various places, the Output window pops up and displays the appropriate text string. If you see "Bull's Eye!" or "Nope!" no matter where you drop Chiron, you know something is wrong with your script. Otherwise, the script works, and you'll see a mix of responses.Notice that if you drop the chiron instance so that it's half in and half out of chiron_targ, the "Bull's eye!" text appears, but the instance remains half in and half out of the targetnot very pleasing to the eye.[View full size image]

If these coordinates look familiar, it's because they're just a couple pixels from where you placed the chiron_targ_mc instance in Step 1 of this task. These two lines of code snap the chiron_mc instance into place if the user releases Chiron anywhere over the chiron_targ_mc instance.
5. | Replace the TRace("Nope!"); line with the following code: this._x=562; |
These two lines make the chiron_mc instance snap back to its starting point if it's released anywhere outside the parameters of the chiron_targ_mc instance.

6. | Test the movie to verify that it works . |
The chiron_mc instance now snaps into place no matter where it's dropped. If it's dropped over the target, it snaps to the target. If it's dropped anywhere other than the target, it snaps back to its original position. Don't worry if Chiron snaps back to a position that's slightly out of line with the rest of the monsters. You'll fix this problem later in the lessonwith mathematical precision.This snapping to position provides one form of feedback to the user, but you're not going to stop there.
7. | Save your file . |