Moving in 3D Space

Moving in 3D Space

Flash CS4 ActionScript 3.0 Tutorial

 

How to Add 3D Movement to Movie Symbols with Flash ActionScript

 

 

The 3D feature was added to Flash in CS4. To make 3D work you must save your swf files to display in Flash Player 10. For more information, see topic in Flash CS4 section titled, Saving a swf Movie. For information 3D rotation, see topic, Rotating in 3D Space.

As you move in Flash's 3D environment, movement is limited to X, Y and Z directions. To move in 3D space, the object must be a Movie Symbol.

Once you understand how the object moves in 3D space, you can move an object with ActionScript on the stage. In this example we will demonstrate both ways to move an object in 3D.

 

Step One

Open a new Flash document and add a second layer.

Step Two

Name the upper layer Actions and the lower one Content.

Step Three

Click frame one of the Content layer and using the Rectangle tool, add a rectangle to the stage.

Step Four

Select the rectangle and convert it to a Movie Symbol (F8).

Step Five

Select the rectangle assign it an instance name of box_mc in the Properties panel.

Step Six

Open the 3D Position and View section in the Properties panel.

Step Seven

Changing the X value moves the object left and right. Positive values are to the right.

Step Eight

Adjusting the Y value moves the object up and down. The values increase as the object moves down.

Step Nine

Adjusting the Z value moves the object in and out. The values increase as the object moves away towards the horizon.

Step Ten

Option double-click (PC - Alt double-click) frame one of the Actions layer.

Step Eleven

Copy/Paste the following code to the ActionScript panel.

box_mc.z=100;

addEventListener(Event.ENTER_FRAME, onZMove);

function onZMove(e:Event):void{

box_mc.z-=5;

}

Step Twelve

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The box travels towards you in the Z direction.

Dissecting the Code

The first line of the code at Step Eleven sets the box_mc starting position at 100 in the Z direction.

An event listener is added to make the function onZMove fire at the project's current frame rate. If the fla file was set to 20 frames per second, the function would run 20 times per second.

Each time the function runs, the box moves 5 units in the Z direction.

 

Stock Images