Flash as3 Practical Formula

Source: Internet
Author: User
Tags acos

Calculation of basic trigonometric functions:
Sine value of the angle = right side/oblique side
Cosine of the angle = adjacent side/oblique side
Tangent of the angle = peer/adjacent edge

Converts radians to degrees and degrees to radians:
Radian = angle * Math. PI/180
Angle = radian * 180/Math. Pi

Rotate to the mouse (or any point:
// Replace mousex and Mousey with the X and Y coordinates to be rotated.
DX = mousex-Sprite. X;
DY = mousey-Sprite. Y;
Sprite. Rotation = math. atan2 (dy, dx) * 180/Math. Pi;

Create a waveform:
// Assign X, Y, or other attributes to a Sprite video or video clip,
// Serves as the drawing coordinate, and so on.
Public Function onenterframe (Event: Event ){
 Value = center + math. Sin (angle) * range;
 Angle + = speed;
}

Create a circle:
// Assign X, Y, or other attributes to a Sprite video or video clip,
// Serves as the drawing coordinate, and so on.
Public Function onenterframe (Event: Event ){
 Xposition = centerx + math. Cos (angle) * radius;
 Yposition = centery + math. Sin (angle) * radius;
 Angle + = speed;
}

Create an ellipse:
// Assign X, Y, or other attributes to a Sprite video or video clip,
// Serves as the drawing coordinate, and so on.
Public Function onenterframe (Event: Event ){
 Xposition = centerx + math. Cos (angle) * radiusx;
 Yposition = centery + math. Sin (angle) * radiusy;
 Angle + = speed;
}

Obtain the distance between two points:
// X1, Y1, X2, and Y2 are two vertices.
// It can also be sprite/movieclip coordinates, mouse coordinates, and so on.
DX = x2-x1;
DY = Y2-Y1;
Dist = math. SQRT (dx * dx + dy * Dy );

Convert hexadecimal to decimal:
Trace (hexvalue );

Convert decimal to hexadecimal:
Trace (decimalvalue. tostring (16 ));

Color combination:
Color24 = red <16 | green <8 | blue;
Color32 = Alpha <24 | red <16 | green <8 | blue;

Color extraction:
Red = color24> 16;
Green = color24> 8 & 0xff;
Blue = color24 & 0xff;
Alpha = color32> 24;
Red = color32> 16 & 0xff;
Green = color32> 8 & 0xff;
Blue = color0000& 0xff;

Draw a curve through a certain point:
// XT, yt is the point we want to pass through
// X0, y0, and X2. Y2 are the two ends of the curve.
X1 = XT * 2-(x0 + x2)/2;
Y1 = yt * 2-(y0 + y2)/2;
MoveTo (x0, y0 );
Curveto (x1, Y1, X2, Y2 );

Convert the angular velocity to X and Y speed:
VX = speed * Math. Cos (angle );
Vy = speed * Math. Sin (angle );

Conversion of angle acceleration (force acting on objects) to X and Y acceleration:
Ax = force * Math. Cos (angle );
Ay = force * Math. Sin (angle );

Add acceleration speed:
VX + = ax;
Vy + = Ay;

Add the speed to the coordinates:
Movieclip. _ x + = VX;
Sprite. Y + = Vy;

Remove an outbound object:
If (sprite. X-Sprite. width/2> right |
Sprite. x + Sprite. width/2 <left |
Sprite. Y-Sprite. Height/2> bottom |
Sprite. Y + Sprite. Height/2 <top)
{
 // Delete the video code
}

Reset the outbound object:
If (sprite. X-Sprite. width/2> right |
Sprite. x + Sprite. width/2 <left |
Sprite. Y-Sprite. Height/2> bottom |
Sprite. Y + Sprite. Height/2 <top)
{
 // Reset the position and speed of the video.
}

Screen surround outbound objects:
If (sprite. X-Sprite. width/2> right)
{
 Sprite. x = left-Sprite. width/2;
}
Else if (sprite. x + Sprite. width/2 <left)
{
 Sprite. x = right + Sprite. width/2;
}
If (sprite. Y-Sprite. Height/2> bottom)
{
 Sprite. Y = Top-Sprite. Height/2;
}
Else if (sprite. Y + Sprite. Height/2 <top)
{
 Sprite. Y = bottom + Sprite. Height/2;
}

Friction application (correct method) :
Speed = math. SQRT (VX * VX + Vy * Vy );
Angle = math. atan2 (Vy, VX );
If (speed> friction)
{
 Speed-= friction;
}
Else
{
 Speed = 0;
}
VX = math. Cos (angle) * speed;
Vy = math. Sin (angle) * speed;

Friction application (Simple Method)
VX * = friction;
Vy * = friction;

Simple slow motion, long shape:
VaR DX: Number = targetx-Sprite. X;
VaR DY: Number = targety-Sprite. Y;
VX = DX * easing;
Vy = Dy * easing;
Sprite. x + = VX;
Sprite. Y + = Vy;

Simple easing, center:
VX = (targetx-Sprite. X) * easing;
Vy = (targety-Sprite. Y) * easing;
Sprite. x + = VX;
Sprite. Y + = Vy;

Simple easing, short:
Sprite. x + = (targetx-Sprite. X) * easing;
Sprite. Y + = (targety-Sprite. Y) * easing;

Simple elastic movement, long shape:
VaR ax: Number = (targetx-Sprite. X) * spring;
VaR ay: Number = (targety-Sprite. Y) * spring;
VX + = ax;
Vy + = Ay;
VX * = friction;
Vy * = friction;
Sprite. x + = VX;
Sprite. Y + = Vy;

Simple elastic movement, center shape:
VX + = (targetx-Sprite. X) * spring;
Vy + = (targety-Sprite. Y) * spring;
VX * = friction;
Vy * = friction;
Sprite. x + = VX;
Sprite. Y + = Vy;

Simple elastic movement, short shape:
VX + = (targetx-Sprite. X) * spring;
Vy + = (targety-Sprite. Y) * spring;
Sprite. x + = (VX * = friction );
Sprite. Y + = (Vy * = friction );

Offset elastic movement:
VaR DX: Number = Sprite. X-fixedx;
VaR DY: Number = Sprite. Y-fixedy;
VaR angle: Number = math. atan2 (dy, dx );
VaR targetx: Number = fixedx + math. Cos (angle) * springlength;
VaR targety: Number = fixedx + math. Sin (angle) * springlength;
// For example, The auto moves to targetx and targety.

Distance Collision Detection:
// Starts with spritea and spriteb
// If a blank film is used, or the film does not have the radius attribute
// The width or height can be divided by 2.
VaR DX: Number = spriteb. X-spritea. X;
VaR DY: Number = spriteb. Y-spritea. Y;
VaR Dist: Number = math. SQRT (dx * dx + dy * Dy );
If (Dist <spritea. radius + spriteb. radius)
{
 // Handle collision
}

Multi-object collision detection:
VaR numobjects: uint = 10;
For (var I: uint = 0; I <numobjects-1; I ++)
{
 // Use variable I to extract reference
 VaR objecta = objects [I];
 For (var j: uint = I + 1; j
 {
   /// Use variable J to extract reference
   VaR objectb = objects [J];
   // Perform Collision Detection
   // Between objecta and objectb
 }
}

Coordinate rotation:
X1 = math. Cos (angle) * X-math. Sin (angle) * Y;
Y1 = math. Cos (angle) * Y + math. Sin (angle) * X;

Inverse coordinate rotation:
X1 = math. Cos (angle) * x + math. Sin (angle) *; y
Y1 = math. Cos (angle) * Y-math. Sin (angle) * X;

Mathematical Expression of Momentum Conservation:
                     (M0-M1) * V0 + 2 * m1 * V1
V0final = ----------------------------------------------
                                    M0 + M1
                     (M1-M0) * V1 + 2 * M0 * V0
V1final = ---------------------------------------------
                                    M0 + M1

The ActionScript expression for conservation of momentum, short:
VaR vxtotal: Number = vx0-vx1;
Vx0 = (ball0.mass-ball1.mass) * vx0 +
2 * ball1.mass * vx1 )/
(Ball0.mass + ball1.mass );
Vx1 = vxtotal + vx0;

General Formula of gravity:
Force = g * m1 * m2/distance2
Implement the universal gravitation of ActionScript:
Function gravitate (parta: ball, partb: ball): void
{
 VaR DX: Number = partb. X-parta. X;
 VaR DY: Number = partb. Y-parta. Y;
 VaR distsq: Number = DX * dx + dy * dy;
 VaR Dist: Number = math. SQRT (distsq );
 VaR force: Number = parta. Mass * partb. Mass/distsq;
 VaR ax: Number = force * dx/Dist;
 VaR ay: Number = force * dy/Dist;
 Parta. VX + = AX/parta. mass;
 Parta. Vy + = Ay/parta. mass;
 Partb. VX-= AX/partb. mass;
 Partb. Vy-= Ay/partb. mass;
}

Cosine theorem
A2 = B2 + C2-2 * B * C * Cos
B2 = a2 + C2-2 * a * C * Cos B
C2 = a2 + B2-2 * a * B * Cos C

Cosine theorem of ActionScript:
A = math. ACOs (B * B + C * C-A * A)/(2 * B * C ));
B = math. ACOs (A * A + C * C-B * B)/(2 * a * C ));
C = math. ACOs (A * A + B * B-c * C)/(2 * a * B ));

Basic Methods:
Scale = fL/(FL + zpos );
Sprite. scalex = Sprite. scaley = scale;
Sprite. Alpha = scale; // optional
Sprite. x = vanishingpointx + xpos * scale;
Sprite. Y = vanishingpointy + ypos * scale;

Z sorting:
// Assume there is an array of 3D objects with the zpos attribute
Objectarray. sorton ("zpos", array. Descending | array. Numeric );
For (var I: uint = 0; I <numobjects; I ++)
{
 Setchildindex (objectarray [I], I );
}

Coordinate rotation:
X1 = cos (anglez) * xpos-sin (anglez) * ypos;
Y1 = cos (anglez) * ypos + sin (anglez) * xpos;
X1 = cos (angley) * xpos-sin (angley) * zpos;
Z1 = cos (angley) * zpos + sin (angley) * xpos;
Y1 = cos (anglex) * ypos-sin (anglex) * zpos;
Z1 = cos (anglex) * zpos + sin (anglex) * ypos;

3D distance:
    Dist = math. SQRT (dx * dx + dy * dy + DZ * Dz );

 

It finally came to an end... After several months, I have reprinted the foundation ationsript 3.0 animation.

Some of your own opinions are inserted in it. If you have any questions, please submit them.

 

 

(If You Want To reprint it, please specify the source at http://blog.sina.com.cn/jooi. Thank you)

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.