Back to "flash Basic Theory Class-Catalog"
In the 15th chapter, we introduce the third dimension, but just place the object in the 3D space and set the size and position. The object is actually still 2D. It's like the old 3D game where we can go around an object or character and these objects will turn around and face us. These objects or characters are not really going to turn around-just look like this because they are all 2D objects and that's the only angle we see.
In this chapter, we will really create the 3D model in Flash. Specifically create and use 3D dots, lines, fills, and stereo graphics. After learning this chapter, you can create various shapes in three-dimensional space and move and rotate them.
Create Dots and lines
It doesn't make sense to create a point in 3D only and not create a line. Because theoretically, a point is not a dimension and is not visible. In the beginning, we will also use the Ball3d class, give a very small radius, as long as you can see where the point is enough. This way, you just need to create a few more lines to connect the balls together. Quite simple. We've done this before, but this time we're going to add a perspective to the ball and put them into 3D space.
You can set the instance of the point to black, 10 pixel wide. Create instances of points, rotate them according to the position of the mouse (as described in the last chapter), and draw some lines between the dots. The code is almost the same as the rotatexy.as file in the previous chapter. The main difference is at the end of the Onenterframe, drawing the line from the first point to all the remaining points. At the same time, I also deleted the Sortz method, because here is not the same. The following is the entire code for lines3d1.as. The results of the operation are shown in Figure 16-1.
Package {
Import Flash.display.Sprite;
Import flash.events.Event;
public class Lines3d1 extends Sprite {
private Var Balls:array;
private var numballs:uint = 50;
private var fl:number = 250;
private var vpx:number = STAGE.STAGEWIDTH/2;
private var vpy:number = STAGE.STAGEHEIGHT/2;
Public Function lines3d1 () {
Init ();
}
Private Function init (): void {
Balls = new Array ();
for (var i:uint = 0; i < numballs; i++) {
var Ball:ball3d = new Ball3d (5, 0);
Balls.push (ball);
Ball.xpos = Math.random () * 200-100;
Ball.ypos = Math.random () * 200-100;
Ball.zpos = Math.random () * 200-100;
AddChild (ball);
}
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:uint = 0; i < numballs; i++) {
var ball:ball3d = balls[i];
Rotatex (ball, Anglex);
Rotatey (ball, Angley);
Doperspective (ball);
}
Graphics.clear ();
Graphics.linestyle (0);
Graphics.moveto (balls[0].x, BALLS[0].Y);
for (i = 1; i < numballs; i++) {
Graphics.lineto (balls[i].x, BALLS[I].Y);
}
}
Private Function Rotatex (Ball:ball3d, anglex:number): void {
var cosx:number = Math.Cos (Anglex);
var sinx:number = Math.sin (Anglex);
var y1:number = Ball.ypos * Cosx-ball.zpos * SINX;
var z1:number = Ball.zpos * cosx + ball.ypos * SINX;
Ball.ypos = y1;
Ball.zpos = z1;
}
Private Function Rotatey (Ball:ball3d, angley:number): void {
var cosy:number = Math.Cos (Angley);
var siny:number = Math.sin (Angley);
var x1:number = Ball.xpos * Cosy-ball.zpos * siny;
var z1:number = Ball.zpos * cosy + ball.xpos * siny;
Ball.xpos = x1;
Ball.zpos = z1;
}
Private Function Doperspective (Ball:ball3d): void {
if (Ball.zpos >-FL) {
var scale:number = FL/fl + ball.zpos;
Ball.scalex = Ball.scaley = scale;
ball.x = VPX + ball.xpos * scale;
BALL.Y = vpy + ball.ypos * scale;
Ball.visible = true;
} else {
Ball.visible = false;
}
}
}
}
Figure 16-1 3D Dots and lines