One of the cool new features of Flash CS4 Professional is the ability to animate two-dimensional objects in three-dimensional space (see Figure 5). New tools in the authortime environment and new properties in the DisplayObject class and geom package allow for projecting objects into perspective.

Figure 5. Rectangular shape rotated in three-dimensional space using the 3D Rotation tool
The rotation effect seen in Figure 5 can
be achieved in ActionScript using the new properties of the DisplayObject
class. All sprites and movie clips are display objects in ActionScript 3.0. You're
probably familiar with using the x and y property of the
MovieClip object in older versions of the language. In Flash CS4 Professional,
the x and y properties are joined by the z property, representing
depth.
Movie clips and sprites have access to the following transformational properties:
xyzrotationrotationXrotationYrotationZscaleXscaleYscaleZtransformNotice that the rotation property has been split into three
separate properties in addition to the transitional rotation property. The transform property accepts a matrix for more
advanced multi-property transformations.
As you can see, the updates come in the
form of the new z properties. The z property deals with distance
from a point of view. An object whose z property is 100
is further away from your viewpoint than an object whose z property is 50. As seen in Figure 5, if
the z coordinates of the top of the rectangle are greater than the z coordinates of the bottom of the rectangle, the shape appears to lean back,
creating a rotational effect. Using the rotation properties, you can easily
create these type of effects on your two-dimensional graphics.
It's easy to use the rotation properties to create some interesting animation effects. The following example uses an enterFrame loop to generate the continuous animation of a two-dimensional object in three-dimensional space:
Copy and paste the following code into the Actions panel:
function enterFrameHandler(event:Event):void
{
sq_mc.rotationY += 5;
}
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
Return to the code and change the rotationY value to a rotationZ value:
function enterFrameHandler(event:Event):void
{
sq_mc.rotationZ += 5;
}
addEventListener(Event.ENTER_FRAME,
enterFrameHandler);
Return to the code and combine the rotationX and rotationZ property to see the z axis appear in perspective:
function enterFrameHandler(event:Event):void
{
sq_mc.rotationY += 5;
sq_mc.rotationZ += 5;
}
addEventListener(Event.ENTER_FRAME,
enterFrameHandler);
Note: The ENTER_FRAME event is a part of all sprites and movie clips. When an event handler is added to the timeline of either of these types of objects, the related event handler function is called repeatedly at the frame rate. You can speed the animation up or slow it down by changing the frame rate of the movie in the Property inspector.
Updates to the geom package in ActionScript 3.0 include a number of classes devoted to working with 3D transformations:
The Matrix3D class allows you to perform transformations to all three axes at the same time, which can yield greater performance and code efficiency. The PerspectiveProjection class provides an easy way to apply a 3D transformation to an object or view without explicitly building a Matrix3D instance. The Orientation3D class, Utils3D class, and Vector3D class provide supporting utilities and objects to aid in creating the transformation.
It's easy to use the rotation properties to create some interesting animation effects. The following example uses an enterFrame loop to generate the continuous animation of a two-dimensional object in three-dimensional space:
Copy and paste the following code into the Actions panel:
// Set position in the z axis
square1.z = 200;
square2.z = 500;
// Flag while dragging circle
var isDragging:Boolean = false;
// Change the projection center in relation to the
// circle while it's being dragged...
function dragPressHandler(event:Event)
{
isDragging = true;
focalPt.startDrag();
}
function dragMoveHandler(event:Event)
{
if( isDragging ){
root.transform.perspectiveProjection.projectionCenter = new Point(focalPt.x, focalPt.y);
}
}
function dragReleaseHandler(e:Event)
{
focalPt.stopDrag();
isDragging = false;
root.transform.perspectiveProjection.projectionCenter = new Point(focalPt.x, focalPt.y);
}
focalPt.addEventListener(MouseEvent.MOUSE_DOWN, dragPressHandler);
focalPt.addEventListener(MouseEvent.MOUSE_MOVE, dragMoveHandler);
focalPt.addEventListener(MouseEvent.MOUSE_UP, dragReleaseHandler);
Notice that the first thing the script does is place the two-square back in space (z axis) in relation to the focalPt instance, which has no adjustment to its z axis.
For more information on working with the PerspectiveProjection class, see Projecting 3D objects onto a 2D view in the Flash documentation.