Drag-and-Drop with a Drop Target


For the garbage to be able to drop into the trash can, the garbage needs to know where the trash can is located. Right now, the trash can is a miscellaneous movie clip on the Stage; it needs an instance name so that you can talk to it.

After you've assigned an instance name to the trash can movie clip, you can use the _droptarget property of the garbage movie clip you're dragging to control what happens next .

The _dropTarget property returns the full path to the instance of the object on which you drop. Unfortunately, the path returned is in the deprecated slash syntax. You can get around that by using the eval() statement to convert the slash syntax to dot syntax. After you've done that, you can easily compare the instance name of the trash can movie clip (_root.trashcan) to the name of whatever you drop on. If it turns out that you have dropped on top of the trash can movie clip, you just need to make the current piece of trash invisible.

Now that you know what to do, give it a try.

Exercise 17.2 Using _dropTarget

You already know that you need to assign the trash can an instance name before you do anything else, so start there.

  1. Open the Instance panel (Window > Panels > Instance), select the trash can on the Stage, and give it an instance name of trashcan.

  2. Double-click dead fish MC to open it in Symbol-Editing mode. Select the dead fish button and launch the Actions panel.

  3. If you're dropping the garbage on the trash can, you want to make the garbage movie clip invisible. Add a new line after the stopDrag() action. All you need to do is add an if statement:

     if (eval(this._droptarget) == _root.trashcan){     this._visible = false;  } 

    Your completed code should look like this:

     on (press) {     startDrag ("");  }  on(release){     this.stopDrag();      if (eval(this._droptarget) == _root.trashcan){         this._visible = false;      }  } 
  4. Save and test your movie. The dead fish should disappear when you drop it on the trash can.

  5. Repeat Steps 2 and 3 for the soda can and crumpled paper movie clips. Alternately, you can just copy the code from the dead fish button and paste it in the other two buttons ' Action lists.

Tip

Any time you've entered some code, you can do a quick syntax check by pressing the Options arrow in the top-right corner of the Actions panel and selecting Check Syntax. This can help you catch any errors before you test your movie.


What else could you do with this movie? You could make the different pieces of garbage do something on a mouseover or hit. You could add sound. You could make the trash can open and close.

You also could set it up so that you could drop the garbage only on the ground or in the trash canno leaving the garbage hanging in the sky. You're limited only by your imagination .

Next you combine the drag-and-drop interaction you already know with creating a custom cursor.



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net