Chapter 8 easing and elastic movement (2) (as3.0)

Source: Internet
Author: User
Tags addchild

Spring Link
     Next we will concatenate several elastic balls. We briefly discussed
The concept means that an object follows the mouse, and another object follows the object, and so on. At that time
For example,It seems that this effect is somewhat inferior. However, when we use this concept in elastic motion,
The effect is completely different.
     The design concept of this program: create three small balls named ball0, ball1, and ball2. The first ball, ball0
The action is the same as the action in the preceding example. Ball1 moves to ball0 and ball2 to ball1. Each
The ball is affected by gravity, so they all fall down. The Code is a little complicated. The document class chain.:
Package {
 Import flash. display. Sprite;
 Import flash. Events. event;
 Public class chain extends sprite {
  Private var ball0: ball;
  Private var ball1: ball;
  Private var ball2: ball;
  Private var spring: Number = 0.1;
  Private var friction: Number = 0.8;
  Private var gravity: Number = 5;
  Public Function chain (){
    Init ();
  }
  Private function Init (): void {
    Ball0 = new ball (20 );
    Addchild (ball0 );
    Ball1 = new ball (20 );
    Addchild (ball1 );
    Ball2 = new ball (20 );
    Addchild (ball2 );
    Addeventlistener (event. enter_frame, onenterframe );
  }
  Private function onenterframe (Event: Event): void {
    Moveball (ball0, mousex, Mousey );
    Moveball (ball1, ball0.x, ball0.y );
    Moveball (ball2, ball1.x, ball1.y );
    Graphics. Clear ();
    Graphics. linestyle (1 );
    Graphics. moveTo (mousex, Mousey );
    Graphics. lineto (ball0.x, ball0.y );
    Graphics. lineto (ball1.x, ball1.y );
    Graphics. lineto (ball2.x, ball2.y );
  }
  Private function moveball (ball: ball, targetx: Number, targety: Number): void {
    Ball. VX + = (targetx-ball. X) * spring;
    Ball. Vy + = (targety-ball. Y) * spring;
    Ball. Vy + = gravity;
    Ball. VX * = friction;
    Ball. Vy * = friction;
    Ball. x + = ball. VX;
    Ball. Y + = ball. Vy;
  }
 }
}
   Taking a look at the ball class, we found that each object instance has its own VX and Vy attributes, and their initial
The start value is 0. Therefore, in the init method, we only need to create small balls and add them to the display list.
   Then, the onenterframe function enables elastic motion. Here we call the moveball method.
                         The parameters of this function are the X and Y coordinates of a ball object and the target point respectively.
It is much easier to create three-step motion code.
Each ball calls this function. The first ball uses the X and Y of the mouse as the target position, and the second third ball uses
The first and second balls serve as the target positions.
   Finally, after determining the positions of all the small balls, draw the line. The starting point of the draw line is the mouse position, and then draw the line in sequence.
In this way, every ball is connected to all the balls. Note that the friction in the program is reduced to 0.8
So that the ball can quickly stabilize.
Create an array to save the reference of all objects in the chain, and traverse each ball in the array cyclically and execute the motion,
Make this program more flexible. Here we only need to make some small changes. First, two new variables are required to represent the array and
Number of objects:
Private var bils: array;
Private var numbils: Number = 5;
In the init function, use the for loop to create all objects and add object references to the array:
Private function Init (): void {
Bols = new array ();
   For (var I: uint = 0; I <numbils; I ++ ){
       VaR ball: ball = new ball (20 );
       Addchild (ball );
       Bils. Push (ball );
   }
   Addeventlistener (event. enter_frame, onenterframe );
}
   Finally, the onenterframe method has the largest variation. First, set the line to move the starting point of the drawing to the mouse position,
Go to the first ball, and cycle to set the position and link the remaining ball. By changing the numbils variable, we
You can add any number of small balls.
Private function onenterframe (Event: Event): void {
   Graphics. Clear ();
   Graphics. linestyle (1 );
   Graphics. moveTo (mousex, Mousey );
   Moveball (bils [0], mousex, Mousey );
   Graphics. lineto (bils [0]. X, bils [0]. y );
   For (var I: uint = 1; I <numbils; I ++ ){
       VaR Balla: ball = bils [I-1];
       VaR ballb: ball = bils [I];
       Moveball (ballb, Balla. X, Balla. y );
    

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.