Add the following lines at the end of the current script:
this.onMouseMove = function() {
if (_xmouse > leftBoundary && _ymouse > topBoundary && ¬
_xmouse < rightBoundary && _ymouse < bottomBoundary) {
basketball_mc.startDrag(true);
} else {
stopDrag();
}
}

Using an
onMouseMove event handler, the
if statement is analyzed each time the mouse is moved.
With this
if statement, we're checking to determine that four conditions are true. If they are, dragging will commence; if not, dragging will cease. We're checking the current horizontal and vertical positions of the mouse pointer (
_xmouse and
_ymouse, respectively) to see how they compare with the boundaries we defined earlier.
Let's look at a couple of possible scenarios to understand the logic behind this
if statement. Suppose that during playback of the movie, the mouse pointer is moved to where its horizontal position (
_xmouse) is 347 and its vertical position (
_ymouse) is 285. By plugging in these values as well as the values that define our boundaries, the
if statement would look similar to the following:
if (347 > 60 and 347 < 490 and 285 > 220 and 285 < 390)
In this circumstance, the
if statement would evaluate to
TRue because all the conditions are true347 is greater than 60 and less than 490, and 285 is greater than 220 and less than 390. In this scenario, dragging is allowed.
Let's look at one more scenario. Suppose that during playback of the movie, the mouse pointer is moved to a horizontal position of 42 and a vertical position of 370. If we plug in these values, the
if statement looks like this:
if (42 > 60 and 42 < 490 and 370 > 220 and 370 < 390)
In this circumstance, the
if statement evaluates to
false because not all the conditions are true42 is
not greater than 60 (the first condition in the statement).
When the
if statement evaluates to
true, the
startDrag() action is triggered and the
basketball_mc instance becomes draggable. The
true parameter value used in this action causes the center of the
basketball_mc movie clip instance to be locked to the vertical and horizontal positions of the mouse pointer.
Note
The startDrag() action is not the only way to drag a movie clip instance. In our script, we could replace this action with the following:
basketball_mc._x = ._xmouse;
basketball_mc._y = ._ymouse;
These two lines would cause the x and y coordinates of the basketball_mc movie clip instance to mimic the x and y coordinates of the mouse pointer, so it appears to be dragged. The advantage of this technique is that it allows you to drag multiple movie clip instances simultaneously. In contrast, the startDrag() action allows only one movie clip instance at a time to be dragged. In our script, this is sufficient because the basketball is the only item that needs to be draggable.
When the
if statement evaluates to
false, the
stopDrag() action is triggered, causing the ball to stop being dragged. Because this
if statement is evaluated with each movement of the mouse, the dragging process can be stopped and started frequently, depending on the current position of the mouse pointer.