Rendering 3D objects in Flash

Step 3

Writing actionscript code

  1. Create a new movie
  2. Create a new movieclip with the "insert/New Symbol..." cmd and draw a line.
    Using the Transform panel, make the line 100 pixels wide and 100 px high.
    At the X,Y locations type 0. Now the upper left end of the line is aligned with the movieclip's point of origin.
  3. Now we will use a slightly altered version of William Tagliaferro's 3d Math library
    Copy the following code and paste it into a text file named "3dmath.as"
    The file should be placed in the same directory with the .fla file


    I won't get into in-depth details of how vector rotations and transformations are done, later on you can find some very inderesting links of articles fully covering this topic. You can skip these explanations if you want and move to the next step.
    The general idea is that new coordinates of the old vertex can derive from the old ones with the multiplication of the [x,y,z] matrix with a transformation matrix.

    Where Xan stands for the rotation angle about X axis, Yan stands for rotation angle about Yaxis, Zan for the rotation angle about Z axis Just a brief description of 3dmath.as functions:
  4. Create a new movie clip with the "Insert/New Symbol..." command and name it 3dclip
  5. Now we will use the library in our movieclip
    Drag the line movieclip into the stage, and give it a duration of 5 frames. Name the istance of this movieclip "mline"
    Create a seperate layer and name it "actions"
  6. Create a keyframe on frame 1 of the actions layer, and type the following code:
    #include "3DMath.as"
    displaypoints=false;

    The displaypoints attribute defines the appearance of the object; if it is set to true then you will see only its vertices. If it is set to false you will see the object as a wireframe.
  7. Set a keyframe on frame 2 of the actions layer and paste the text you have in MS Word.
  8. Set a keyframe on frame 2 of the actions layer and paste the following actions:

    These functions move the object with the arrow keys, rotate object points on the screen, and connect them with lines, if displaypoints is set to false.
  9. Set a keyframe on frame 4 of the actions layer and type the following code:
    gotoAndPlay (3);
    This ensures repeated execution of the rendering functions which reside in the actions of frame 3.
  10. You must make now a movieclip which will be used to represent each vertex on the screen, in case displaypoints parameter is set to true
    Create a new movieclip, and assign it a name. Draw a circle which is aligned to the center of the stage and paint it with any solid color or gradient you want. Right click your movieclip on the library and choose "Linkage..." .
    Select "Export this symbol" and type in the "Identifier" input box the name "sphere"
  11. Return to the scene edit mode and drag your movieclip onto the stage
  12. Test your movie
  13. Pat yourself on the back; you have done a good job!

    If something doesn't work or you're just too lazy, you can download the files used in this tutorial here

Interesting Links

For those who want to study further about 3d graphics implementation, the following links are available:
3D Graphics Implementation
geometry for computer graphics, notes
ITTI Computer Graphics & Visualization
Vector Math Tutorial for 3D Computer Graphics
Viewing Objects in Computer Graphics - An Application of Matrices

Comments, Suggestions?
Email me drsoulis@yahoo.com