AS Function Code tutorial

Source: Internet
Author: User
Tags acos cos pow sin

Distance between two points
Math. sqrt (Math. pow (p1. _ x-p2. _ x), 2) + Math. pow (p1. _ y-p2. _ y), 2 ))
You can use this formula to calculate the distance between a point and the origin:
Because the origin coordinate is (0, 0), the formula deformation is as follows:
Math. sqrt (p1. _ x * p1. _ x + p1. _ y * p1. _ y)
Here we will use these two formulas to make some results.
Example 1: rotating pointer
Ideas:
There are many methods to calculate the & theta; of an angle, such:
Distance.jpg
Sin & theta; = y/r
Cosine function cos & theta; = x/r
Tangent function tan & theta; = y/x
Cotangent function cot & theta; = x/y
Cut function sec & theta; = r/x
Cosine cut function csc & theta; = r/y
All these trigonometric functions can obtain the same angle & theta;
In this example, only atan and acos are used instead of other trigonometric functions, because their return values are all arbitrary numbers, while the return values of other functions are limited numbers.
Step 1:
Draw a pointer and save it as a video clip. The registration point is at the root of the pointer. The instance name is "pointer"

Step 2:
Add the AS code (tangent function) to the first frame ):
Pointer. onMouseMove = function (){
Var dx = _ xmouse-this. _ x;
Var dy = _ ymouse-this. _ y;
Var theta = Math. atan2 (dy, dx );
// Use the tangent function to obtain the angle
This. _ rotation = theta/Math. PI * 180;
// Converts radians to degrees.
};

Add the AS code (arccosine function) to the first frame ):
Pointer. onMouseMove = function (){
Var dx = _ xmouse-this. _ x;
Var dy = _ ymouse-this. _ y;
Var r = Math. sqrt (dx * dx + dy * dy );
// R indicates the straight distance between the point and the mouse. Here, it is also the oblique side r.
Var theta = Math. acos (dx/r) * Math. abs (dy)/dy;
// Use the arccosine function to obtain the angle.
This. _ rotation = theta/Math. PI * 180;
// Converts radians to degrees.
};
Example 2: bubble effect
Ideas:
By judging the moving distance of the mouse, the size of the copied bubble is changed. The farther the bubble is moved, the larger the bubble is.

Step 1:
Draw a bubble, save it as a video clip, and connect to it-> export-> the symbol "bubble"
Step 2:
Add the AS code to the first frame:
Var n: Number = 0;
Var old_x, old_y;
// These two global variables are used to store the previous mouse position
_ Root. onMouseMove = function (){
Var dx = _ xmouse-old_x;
Var dy =_ ymouse-old_y;
Var distance = Math. sqrt (dx * dx + dy * dy );
// Determine the distance between the current mouse position and the previous mouse position
Var p = attachMovie ("bubble", "B" + n, n );
P. _ x = _ xmouse;
P. _ y = _ ymouse;
P. _ width = p. _ height = distance;
// Use the obtained distance to set the bubble size.
P. onEnterFrame = function (){
If (this. _ alpha> 0 ){
This. _ xscale = this. _ yscale + = 5;
This. _ alpha-= 5;
// Make a bubble to become larger and disappear
} Else {
Delete this. onEnterFrame;
RemoveMovieClip (this );
// After the bubble is invisible, do not forget to delete the film and function. Otherwise, the film will become stuck.
  }
};
Old_x = _ xmouse;
Old_y = _ ymouse;
// Save the mouse position as the previous mouse position
N ++;
};

Example 3: radiation point effect
Ideas:
1. Evenly distribute points to the stage in the form of columns
2. Use the distance formula between points to determine the distance between each point and the Mouse
3. Zoom value = distance-120

4. When the distance is 120 <220, the scaling value starts to decrease ~ 0], as shown in inner
5. When the distance is less than 120, the scaling value is smaller than 0, indicating that the system starts to grow inward and begins to go negative.

6. When the distance is 0, the zoom value is-120, which is the circle with the larger size at the center of the mouse.
Step 1:
Draw a black spot with a size of 20*20 and save it as a video clip,
The registration point is in the center, connect-> export-> the identifier "dot"
Step 2:
Var size = 20;
// The black spot size
Var Colum = Math. floor (Stage. width/size );
Var Row = Math. floor (Stage. height/size );
For (var I = 0; I <Colum; I ++ ){
For (var j = 0; j <Row; j ++ ){
Var p = _ root. attachMovie ("dot", "d" + (I * Row + j), I * Row + j );
P. _ x = (size/2) + I * size;
P. _ y = (size/2) + j * size;
// Evenly distributes points to the stage in the form of columns
P. onMouseMove = function (){
Var dx = this. _ x-_ xmouse;
Var dy = this. _ y-_ ymouse;
Var distance = Math. sqrt (dx * dx + dy * dy );
Var d = distance-120;
// If the distance is greater than 220, no scaling is performed. If the distance is smaller than 220, the scaling is performed.
'If (d> 100 ){
D = 100;
   }
// Or d = d> 100? 100: d;
This. _ xscale = this. _ yscale = d;
};
}
}
Example 4: tricky ball
First, review the parameter equation of the circle: x coordinate = R * cos & theta; + m; y coordinate = R * sin & theta; + n
According to the sine functions sin & theta; = y/r and cosine functions cos & theta; = x/r, the parameter equation of the circle is:
X coordinate = R * (x/r) + m; y coordinate = R * (y/r) + n where (m, n) is the origin coordinate.
Ideas:
1. Use the variant circular parameter equation: x = R * (x/r) + m; y = R * (y/r) + n
2. To keep the ball farther away, add the new center coordinates dif_x and dif_y.
3. Obtain the new position of the ball = the initial position of the ball-(center position + position on the circle)
Step 1:
Draw a black spot with a size of 20*20 and save it as a video clip,
The registration point is in the center, connect-> export-> the identifier "dot"
Step 2:
Add the AS code to the first frame:
Var size = 30;
// 10 pixels larger than the dimension of the vertex as the row and column spacing
Var Colum = Math. floor (Stage. width/size)-2;
Var Row = Math. floor (Stage. height/size)-2;
// The number of rows and columns is reduced by 2, so that two rows are left blank.
Var Max = 1000;
// Constant that reflects and affects the circle radius
For (var I = 0; I <Colum; I ++ ){
For (var j = 0; j <Row; j ++ ){
Var p = _ root. attachMovie ("dot", "d" + (I * Row + j), I * Row + j );
P. _ x = p. oldx = 20 + (size/2) + I * size;
P. _ y = p. oldy = 20 + (size/2) + j * size;
// Evenly distributes points to the stage in the form of rows and columns, and saves the initial positions as oldx and oldy.
P. onEnterFrame = function (){
Var dx = _ xmouse-this. _ x;
Var dy = _ ymouse-this. _ y;
Var r = Math. sqrt (dx * dx + dy * dy );
// R indicates the straight distance between the point and the mouse. Here, it is also the oblique side r.
Var Circle_x = (dx/r) * Max/r;
Var Circle_y = (dy/r) * Max/r;
// Calculate the position of the point on the circle. Max/r is the radius, so the smaller the r, the larger the radius.
Var dif_x = (this. oldx-this. _ x)/2;
Var dif_y = (this. oldy-this. _ y)/2;
// The new center is centered on the difference between the new position of the ball and the initial position.
This. _ x = this. oldx-(dif_x + Circle_x );
This. _ y = this. oldy-(dif_y + Circle_y );
// Obtain the new position of the ball = the initial position of the ball-(center position + position on the circle)
// Subtract (center position + circle position): Opposite the ball position to the mouse position
};
}
}

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.