Game Development Reference
The code uses depPaopK^fa_p to check whether the object is over its target. If it is, it then checks
to see whether the object is currently being dragged. You need to check for this so that the object
doesn't snap to the target before the mouse button is released. If it all seems fine, the dragable
objects are assigned the exact same t and u positions as the p]ncapo. The result is that the objects
appear to snap into place.
Centering the drag object to the mouse
The op]np@n]c method has two optional parameters that give you a little more fine control over how
your dragable objects behave.
The first parameter is called hk_g?ajpan. It's a Boolean value that tells AS3.0 whether the object
should be centered over the mouse. To use it, just add pnqa as an argument in the op]np@n]c method,
Try it in the example and you'll notice that the object snaps to the mouse center when you click it.
Confining the drag area
You might find in some circumstances that you want to contain dragging to a certain area of the stage.
The op]np@n]c method has a second optional parameter called ^kqj`o that accepts a Na_p]jcha
object as an argument. The Na_p]jcha object defines the area that the object will be confined to.
First, however, you need to actually make a Na_p]jcha object, which is an abstract bit of code that
defines an area of a rectangle. Creating one is about as easy or difficult as it is to create a Lkejp object
1. Import the Na_p]jcha class with the following eilknp statement:
2. Next, declare a Na_p]jcha object in your class (you can give this variable any name you like;
you don't have to call it [na_p]jcha):
3. Define the rectangle by specifying t, u, daecdp, and se`pd values (you can do this anywhere in
your program, but it probably makes sense to do it in the constructor method):
4. After the [na_p]jcha variable is defined, you can use it as the second argument in the
op]np@n]c method, like this: