Flash basic Theory course seventh Chapter Interactive Animation: Moving Object Ⅱ

Source: Internet
Author: User
Tags variables requires

Back to "flash Basic Theory Class-Catalog"

Throwing

What is the concept of throwing in flash? It means that when we click on an object, we begin to drag it, move it in a certain direction, and then release the mouse and move the object along the drag.

In the process of dragging, you need to determine the speed vector needed to move the object, and then assign the value of the speed vector to the object after the mouse is released. In other words, if you drag the movie 10 pixels to the left, when the mouse is released, its speed vector should be VX =-10.

Setting a new speed vector is no problem for everyone. Just assign the Vx,vy value to the object, as shown in Figure 7-1, but how to determine the two values requires a little bit of finesse. In fact, the principle of calculating the drag distance and applying the velocity vector is reciprocal. When applying the velocity vector, we add the velocity vector to the position of the original coordinates of the object, thus creating a new position, so this formula can be written as: the old position + velocity vector = new position. In order to calculate the moving velocity vector, just make a simple transformation of the equation: the velocity vector = new position – the old position.

Fig. 7-1 The small ball is dragged to the new position. The velocity vector is the distance from the previous position to the current position

When you drag a movie, a new position is formed at each frame. Subtracts the position of the previous frame from the position of the current frame to know the distance the frame is moving. This is the speed vector that moves the distance each frame!

Let's do an example of simplifying the problem to drag a movie on one axis. On a frame, you know the x position of an object is 150. When the next frame is known, its x position is 170. So, within this frame, the object moves 20 pixels on the x-axis, then the X-speed vector of the object is +20. When the mouse is released, the need for the object can also press the X axis of 20 speed, continue to move. Therefore, VX = 20 should be set.

This requires some changes to the previous class. First, you need to know the speed vector for each frame, so you need to add the handler function for the Enterframe event. A new method needs to be created to compute the drag-and-drop velocity vector, which is called trackvelocity. You also need two variables to store the old x,y locations, and you can name them OLDX and Oldy and declare them as class variables. Once you start dragging, you need to know the location of the ball and store it as OLDX and Oldy. After removing the onenterframe, add the trackvelocity as the listener. The complete contents of the OnMouseDown method are as follows:

private function onMouseDown(event:MouseEvent):void {
    oldX = ball.x;
    oldY = ball.y;
    stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    ball.startDrag();
    removeEventListener(Event.ENTER_FRAME, onEnterFrame);
    addEventListener(Event.ENTER_FRAME, trackVelocity);
}

Then, in the Trackvelocity method, subtract the OLDX and oldy from the current position. This gives the current speed vector, which can be stored directly in VX and VY. The OLDX and Oldy are then reset to the current coordinates of the ball.

private function trackVelocity(event:Event):void {
    vx = ball.x - oldX;
    vy = ball.y - oldY;
    oldX = ball.x;
    oldY = ball.y;
}

Finally, when the ball is released, the Enterframe function is exchanged. Then remove the trackvelocity and add the Onenterframe:

private function onMouseUp(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    ball.stopDrag();
    removeEventListener(Event.ENTER_FRAME, trackVelocity);
    addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.