Datasheet

10
Part 1: Getting Started
The typical flow of such a program involves first initializing the program and then using a listener to
loop through a scripted animation routine. The whole process can be divided into three parts: initialize,
listen and loop:
Initialize: Steps 1.3
Initializing an AS3 program can be a little overwhelming at first. The question that most people ask is
how do I know what imports to bring in? There is no magical pool of knowledge that can help you. It
just takes experience (or reading the documentation, or paying attention to syntax). As you work with
the different Flash and Papervision3D methods you ll become experienced in spotting classes that need
to be imported. For example, if you re creating a Wii game, it s obvious that you ll need to import some
Wii classes.
But, which ones? It depends on which methods you re using to create your program. From experience,
you ll begin to recognize and associate the right classes with their methods. Or better yet, Flex builder
has an auto - complete that automatically adds the classes you need upon auto - completion of a method.
In the example below, you create a ball sprite, so it makes sense that you ll need to import the Sprite
class. After importing the Sprite class, you initialize the position and angle of the ball and then set your
focal length (from experience 300 works well for this example). Then in step 3, you create your ball, give
it a color and set it on the stage using the
addChild method.
1. Start by importing the Sprite class; this is where you re going to draw a ball that you will
animate in 3D.
import flash.display.Sprite;//imports sprite class
2. Next declare your variable s zposition, angle, and focal length.
var zposition:Number = 0;//z position
var myAngle:Number =0;//Angle of ball
var fl:Number = 300; //focal length
3. Next create your ball and add it to the stage.
var ball:Sprite = new Sprite();//instantiates ball sprite
ball.graphics.beginFill(0xFF0000);//Assigns a ball color
ball.graphics.drawCircle(0, 0, 40);//draws your ball at (0,0)
ball.graphics.endFill();//ends the fill
addChild(ball);//adds the ball to the stage
Listen: Step 4
In AS3, the event architecture is bigger, better, and badder (in a good way). Written from the ground up,
it s fast, powerful, and easy to use. It incorporates listener objects to listen for events. Since event
listeners all work the same way, once you understand how to use one of them you understand them all.
This is how they work:
c01.indd 10c01.indd 10 12/14/09 3:03:26 PM12/14/09 3:03:26 PM