« Home « Kết quả tìm kiếm

Dragging a Movie Clip Instance within a Boundary


Tóm tắt Xem thử

- Dragging a Movie Clip Instance within a Boundary.
- Being able to drag the ball movie clip instance around the screen is critical to our project's interactivity.
- The ball's position onscreen will determine the volume and panning of the bouncing sound.
- basketball_mc movie clip instance onscreen, however, our scene would not be realistic because the user could drag and bounce the ball over the crowd, the backboard, and so forth.
- mouse's movement and allowing dragging to occur only when the mouse pointer is within a certain area onscreen..
- Continue using the file you were working with at the end of the preceding exercise..
- that is, how to drag the ball movie clip instance in sync with the mouse movement, and how to constrain that dragging to a specific area onscreen..
- As shown by the arrows, all coordinates are based on the distance of that side from the top and left sides of the stage..
- Because we want the basketball to move only when the mouse pointer is within the boundary, in scripting terms we need to check for a condition before the ball can be dragged.
- Logically, this might be translated as follows: If the mouse pointer's position is within the coordinates of the boundary, drag the.
- basketball_mc movie clip instance.
- We'll need to instruct the script to check for this condition on a regular basis because the mouse is in frequent motion.
- Using the onMouseMove event handler, we can check for this condition each time the mouse is moved.
- This will allow our script to act instantly to enable or prevent the basketball_mc movie clip instance from being dragged..
- With the Actions panel open, select Frame 1 of the Actions layer.
- Next, we'll add an if statement that constantly checks the position of the mouse and allows the ball to be dragged only if the mouse pointer is within the boundary we just defined..
- Add the following lines at the end of the current script:.
- basketball_mc.startDrag(true);.
- Using an onMouseMove event handler, the if statement is analyzed each time the mouse is moved..
- We're checking the current horizontal and vertical positions of the mouse pointer (_xmouse and _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 pointer is moved to where its horizontal position (_xmouse) is 347 and its vertical position.
- By plugging in these values as well as the values that define our boundaries, the if statement would look similar to the following:.
- In this circumstance, the if statement would evaluate to true because all the conditions are true—347 is greater than 60 and less than 490, and 285 is greater than 220 and less than 390.
- 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:.
- In this circumstance, the if statement evaluates to false because not all the.
- 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..
- The startDrag() action is not the only way to drag a movie clip instance.
- 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.
- 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..
- When the mouse pointer is moved within the boundary we defined, dragging will occur, causing the ball to appear as if it's bouncing around the court.
- Move the mouse pointer outside

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt