After the seventh line in the script the one with the curly bracket (} ) add the following:
onClipEvent (mouseMove) { if (_root._xmouse > leftBoundary && _root._xmouse < rightBoundary && _root._ymouse > topBoundary && _root._ymouse < bottomBoundary) { startDrag (this, true); } else { stopDrag (); } }
Using a mouseMove event handler, our 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 (_root.xmouse and _root.ymouse, respectively) to see how they compare to 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 is moved to a point where its horizontal position (_root.xmouse ) is 347 and its vertical position (_root.ymouse ) is 285. By plugging in these values as well as the values that define our boundaries, the if statement would look like this:
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 true 347 is greater than 60, 347 is less than 490, 285 is greater than 220, and 285 is 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 is moved to a horizontal position of 42 and a vertical position of 370. By plugging in these values, the if statement would look like this:
if (42 > 60 and 42 < 490 and 370 > 220 and 370 < 390)
In this circumstance, the if statement would evaluate to false because not all the conditions are true that is, 42 is not greater than 60 (the first condition in the statement).
When this if statement evaluates to true, the startDrag() action is triggered. This action has two parameters, separated by a comma:
startDrag (target to drag, lock to center)
Because this script resides on the basketball movie clip instance and we want to drag this same movie clip instance when the if statement evaluates to true , the target to drag is defined as this . The lock to center is set to true so that when dragging occurs, the center of the basketball movie clip instance is locked to the exact vertical and horizontal position of the mouse.
TIP
The startDrag () action is not the only way to drag a movie clip instance. In our script, we could replace this action with:
this._x = _root._xmouse;
this._y = _root._ymouse;
These two lines would cause the X and Y coordinates of the ball movie clip instance to mimic the X and Y coordinates of the mouse, essentially causing it to appear to be dragged. The advantage of this method is that it allows you to drag multiple movie clip instances simultaneously. In contrast, the startDrag () action allows only a single movie clip instance to be dragged at a time. In our case, this is sufficient because the basketball is the only thing that needs to be draggable.
When the if statement evaluates to false , the stopDrag() action is triggered, causing the ball to stop being dragged. Since this if statement is evaluated with each movement of the mouse, the dragging process may be stopped and started frequently, depending on the current position of the mouse.