1. Create a new flash Actionscript 3 size 500x300px, background black.
2. In the scene, use the rectangular tool (G), set the border width of 3px, draw a square size 40x40px;
3. Place the right key on the square, convert the component (for the movie clip), register the setting as the center;
4. Select components, right button > link; set class to Mybox;
5. Go back to the scene and delete the square; Create a new ActionScript file and enter the following code:
Package {
Import Flash.display.MovieClip;
public class Mybox extends MovieClip {
This is the 3d coordinates of the square.
public var xpos3d:number = 0;
public var ypos3d:number = 0;
public var zpos3d:number = 0;
Public Function Mybox () {
}
}
}
Save As Mybox.as, note that there is a flash file in the same path,
6. Return to the main scene, insert the following code in the first frame:
Stereo Scene Depth
Const MAXIMUM_Z:NUMBER = 500;
Number of squares
Const NUMBER_OF_BOXES:NUMBER = 15;
Creates an array that contains squares;
var boxes:array = new Array ();
View Focus Setting
var focallength:number = 300;
Vanishingpoint is a square vanishing point;
var vanishingpointx:number = STAGE.STAGEWIDTH/2;
var vanishingpointy:number = 20;
3D Square Bottom Position
var floor:number = 80;
First Square depth
var startingdepth:number = maximum_z;
The value of the z-distance between boxes
var zdistance:number = 50;
This loop is positioned from a far to near square.
for (Var i=0 i < number_of_boxes; i++) {
var box:mybox = new Mybox ();
Box.xpos3d = 0;
Box.ypos3d = floor;
Box.zpos3d = startingdepth;
Update the depth of the box;
Startingdepth-= zdistance;
Use the angle formula to calculate the scaling ratio;
var scaleratio = focallength/(focallength + box.zpos3d);
scale coordinate scale;
Box.scalex=box.scaley=scaleratio;
Position the squares in the scene (converted from 3d to 2d)
Box.x=vanishingpointx+box.xpos3d*scaleratio;
Box.y=vanishingpointy+box.ypos3d*scaleratio;
Put a square into an array
Boxes.push (box);
Add a box to a scene
AddChild (box);
}
Select the Debug > Test scenario in the menu as follows:
7. Insert the following code after the above code to generate animation;
Adding animation functions with Enter_frame events
AddEventListener (Event.enter_frame, Enterframehandler);
This function is called for every frame
function Enterframehandler (e:event): void {
for (Var i=0 i < number_of_boxes; i++) {
Turn box into a local variable
var box:mybox = (Mybox) (Boxes[i]);
Reduce depth
box.zpos3d-=5;
if (box.zpos3d<=-focallength) {
The last squares are always in the first place in the array
Box.zpos3d=boxes[0].zpos3d+zdistance;
}
var scaleratio = focallength/(focallength + box.zpos3d);
Box.scalex=box.scaley=scaleratio;
Set Transparency variables
box.alpha=scaleratio-0.5;
Box.x=vanishingpointx+box.xpos3d*scaleratio;
Box.y=vanishingpointy+box.ypos3d*scaleratio;
}
Arrange arrays by depth
Sortz ();
}
This function causes the squares to be arranged correctly
function Sortz (): void {
Boxes.sorton ("Zpos3d", Array.numeric | array.descending);
for (var i:uint = 0; i < number_of_boxes; i++) {
Setchildindex (Boxes[i], i);
}
}