Datasheet

Chapter 1: Understanding Flash3D
11
Call the method addEventListener to listen for an event
Name the event you want to listen for
Name the function you want to execute when the event occurs
So in the code below, you want to iterate your animation periodically. This can be done using the enter
frame event listener or a timer. In this case, you ’ ll listen for the enter frame event. Each time a frame
event occurs the function onEnterFrame is called, as shown in step 4.
Next create your onEnterFrame listener which loops through your equations of motion. This is
the heart of all 3D engines.
addEventListener(Event.ENTER_FRAME, onEnterFrame);//loops equations
Loop: Step 5
Looping is the heart of creating a scripted animation. You ’ re probably familiar with creating frame loops
on Flash ’ s timeline. But in Papervision3D, you don ’ t use the timeline. Everything is done in ActionScript.
But you can still create graphical animation by creating a frame ripper, or use animation script created in
Flash by grabbing the animation elements as classes.
In Step 5, the animation is created by incrementing the angle on each loop. As the angle is incremented
the ball oscillates and the z position changes.
Create the function that will be looped. These are your equations of motion that govern the
perspective as it is changed and converts 3D to 2D (or projects onto the viewport).
function onEnterFrame(event:Event):void{
var scale:Number = fl / (fl + zposition);//scale perspective
myAngle=myAngle+.1;//iterates angle
myB all.x = 300*Math.sin(myAngle)*scale; //ball orbit x
ball.y = 300*Math.cos(myAngle)*scale; //ball orbit y
ball.scaleX = scale;//scales perspective in x
ball.scaleY = scale;//scales perspective in y
zposition = 2000*Math.sin(myAngle/10);} //increments z and changes sign.
Upon every loop, the myAngle variable is iterated and the ball.x and ball.y positions oscillate
sinusoidally. This creates the circling motion of the ball. In the last line of code the
zposition variable
oscillates sinusoidally as well, causing the ball to cycle in and out of the screen. By putting these two
motions together your ball will spiral in and out of the screen in the z direction.
You now put it all together and run the code.
Running the Code
The code (all 18 lines of it) is listed below. Just type it in the Flash ActionScript editor and click control
test. You ’ ll see a red ball oscillating on the z - perspective axis. Not very exciting, but there are a number
of concepts here that you ’ ll use throughout the book.
❑
❑
❑
❑
❑
c01.indd 11c01.indd 11 12/14/09 3:03:26 PM12/14/09 3:03:26 PM










