Learn a little flash (43) Flash 3D Rotation every day

Source: Internet
Author: User

I haven't come here to write an article for a long time. The csdn server has finally been optimized. I can continue to post my flash article this time.

I read a book and successfully debugged the program. Now I can run it to create a three-dimensional effect. Good Program

 

Cude.

Package Tom {

Import flash. display. Sprite;

Import flash. Events .*;

Import flash. display. graphics;

Public class cude extends sprite {

Private var points: array = new array ();

Private var vpx: Number = stage. stagewidth/2;

Private var vpy: Number = stage. stageheight/2;

Private var numpoints: Int = 8;

Private var triangles: array;

Public Function cude (){

Points [0] = new point3d (-100,-100,-100 );

Points [1] = new point3d (100,-100,-100 );

Points [2] = new point3d (100,100,-100 );

Points [3] = new point3d (-100,100,-100 );

Points [4] = new point3d (-100,-100,100 );

Points [5] = new point3d (100,-100,100 );

Points [6] = new point3d (100,100,100 );

Points [7] = new point3d (-100,100,100 );

For (var I: Int = 0; I <numpoints; I ++ ){

Points [I]. setvanishingpoint (vpx, vpy );

Points [I]. setcenter (0, 0,200 );}

Triangles = new array ();

Triangles [0] = New Triangle (points [0], points [1], points [2], 0x6666cc );

Triangles [1] = New Triangle (points [0], points [2], points [3], 0x6666cc); // top

Triangles [2] = New Triangle (points [0], points [5], points [1], 0x66cc66 );

Triangles [3] = New Triangle (points [0], points [4], points [5], 0x66cc66); // back

Triangles [4] = New Triangle (points [4], points [6], points [5], 0xcc6666 );

Triangles [5] = New Triangle (points [4], points [7], points [6], 0xcc6666); // bottom

Triangles [6] = New Triangle (points [3], points [2], points [6], 0xcc66cc );

Triangles [7] = New Triangle (points [3], points [6], points [7], 0xcc66cc); // right

Triangles [8] = New Triangle (points [1], points [5], points [6], 0x66cccc );

Triangles [9] = New Triangle (points [1], points [6], points [2], 0x66cccc); // left

Triangles [10] = New Triangle (points [4], points [0], points [3], 0xcccc66 );

Triangles [11] = New Triangle (points [4], points [3], points [7], 0xcccc66 );

Addeventlistener (event. enter_frame, onenterframe );}

Private function onenterframe (Event: Event): void {

VaR anglex: Number = (mousey-vpy) *. 001;

VaR angley: Number = (mousex-vpx) *. 001;

For (var I: Int = 0; I <numpoints; I ++ ){

VaR point: point3d = points [I];

Point. rotatex (anglex );

Point. rotatey (angley );}

Graphics. Clear ();

For (var k: Int = 0; k <triangles. length; k ++ ){

Triangles [K]. Draw (graphics );

}}}}

 

Point3d.

Package Tom {

Import flash. display. Sprite; I

Mport flash. Events .*;

Public class point3d {

Public var FL: Number = 250;

Private var vpx: Number = 0;

Private var vpy: Number = 0;

Private var CX: Number = 0;

Private var Cy: Number = 0;

Private var CZ: Number = 0;

Public var X: Number = 0;

Public var y: Number = 0;

Public var Z: Number = 0;

Public Function point3d (X: Number, Y: Number, Z: Number)

{This. x = x; this. Y = y; this. z = z;

}

Public Function setcenter (CX: Number, Cy: Number, CZ: Number = 0): void {

This. Cx = Cx; this. Cy = Cy; this. cz = cz ;}

 

Public Function setvanishingpoint (vpx: Number, vpy: Number): void {

This. vpx = vpx; this. vpy = vpy ;}

Public Function get screenx (): number {

VaR scale: Number = fL/(FL + Z + cz );

Return vpx + cx + x * scale ;}

Public Function get screeny (): number {

VaR scale: Number = fL/(FL + Z + cz );

Return vpy + Cy + y * scale;

 

} Public function rotatex (anglex: Number): void {

VaR cosx: Number = math. Cos (anglex );

VaR SiNx: Number = math. Sin (anglex );

VaR Y1: Number = y * cosx-z * SiNx;

VaR Z1: Number = z * cosx + y * SiNx;

Y = Y1; Z = Z1 ;}

 

Public Function rotatey (angley: Number): void {

VaR cosy: Number = math. Cos (angley );

VaR Siny: Number = math. Sin (angley );

VaR X1: Number = x * cosy-z * Siny;

VaR Z1: Number = z * cosy + x * Siny;

X = x1; Z = Z1 ;}

 

Public Function rotatez (anglez: Number): void {

VaR cosz: Number = math. Cos (anglez );

VaR sinz: Number = math. Sin (anglez );

VaR X1: Number = x * cosz-y * sinz;

VaR Y1: Number = y * cosz + x * sinz; X = x1; y = Y1 ;}}}

 

Triangle.

Package Tom {

Import flash. display. graphics;

Public class triangle {

Private var pointa: point3d;

Private var pointb: point3d;

Private var pointc: point3d;

Private var color: uint;

Public Function triangle (A: point3d, B: point3d, C: point3d, color: uint)

{Pointa = A; pointb = B; pointc = C; this. Color = color ;}

Public Function draw (G: Graphics): void {

G. beginfill (color );

G. moveTo (pointa. screenx, pointa. screeny );

G. lineto (pointb. screenx, pointb. screeny );

G. lineto (pointc. screenx, pointc. screeny );

G. lineto (pointa. screenx, pointa. screeny );

G. endfill ();}

}

}

This file is saved in a Tom folder. When we run the file, we only need to bind the document to you.

Write so much for now !! Coming soon

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.