>>week 7 examples
Creating Drag and Drop Elements
The Sprite class has built into it several methods for allowing user input to grab an object on the stage and drag it to another location. These methods are called startDrag and stopDrag, can be used with any Sprite or MovieClip, and are generally triggered by mouse events. These methods are tied to a specific display object and triggered within an event handler.
The startDrag is called when the initial user event is dispatched, and causes the target object to be draggable. This method takes two optional arguments:
- lockcenter – this is a Boolean that determines whether the Sprite is locked to the center of the mouse position (true) or to where the user first clicked on it (false). The default value is false.
- bounds – specifies a boundary rectangle for the Sprite, beyond which in cannot be dragged. The default value is null.
The stopDrag method is called when the terminal user event is dispatched, and causes the target object to cease being draggable. This method takes no arguments.
There is a property of the Sprite object that is related to these functions, called the dropTarget. This specifies the object which the sprite was dragged and released onto.
Homework
Create a simple shape matching game, such as you might give to a young child. There should be at least 5 shapes and 5 matching shape targets. Create the code such that:
- If the user drags the shape to the wrong target, it snaps back to its original position and displays a message that tells the user that is incorrect
- If the user drags the shape to the right target, a message is displayed that tells them they made the correct
Have fun with this and be creative! Create something that would be appealing to a kid, and easy to understand.
Prof.
I can’t come to the class today.
I emailed homework to you.
Regards
XiaoQiong ji