CHAPTER 15
506
Creating 3D Objects
One of the most basic things you can do in Papervision is add a primitive object to your scene. Examples
of primitive objects are a cone, plane, sphere, cylinder, and so on. Let’s start with using BasicView and
adding a sphere.
First create an ActionScript file called SphereExample and have it extend BasicView -
org.papervision3d.view.basicView.
package
{
import org.papervision3d.objects.primitives.Sphere;
import org.papervision3d.view.BasicView;
public class SphereExample extends BasicView
{
private var sphere:Sphere;
public function SphereExample()
{
super();
sphere = new Sphere();
scene.addChild(sphere);
startRendering();
}
}
}
We start this class by extending BasicView to bypass all the setup work. The BasicView class
automatically sets up our camera, renderer, and scene and adds the hooks to allow us to start and stop
rendering our file. Since we are extending the BasicView class, we need to be sure to call super() in the
constructor. Otherwise, our BasicView will be of very little use to us. Our next line of code inside our
constructor creates a sphere that is a primitive object of Papervision. Finally, we add our sphere to the
scene and then call startRendering().
There are two ways to render your scene.
You can use singleRender(), which does exactly what it sounds like. It will render the scene only once.
Using singleRender() is fine if you want a snapshot of what is happening or plan on continually calling it as
needed from a Tween engine, such as TweenLite.
Calling startRendering() starts a frame loop to constantly render your 3D. This is great if you know you’re
always going to have objects in your scene animating. Since 3D in Flash is a processor intensive action
you would want to call stopRendering() as much as possible when the constant render is not needed.
Now that we have our class that extends BasicView, it’s time to add it to our Flex application. Here is our
application code.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="1024" minHeight="768"

Get AdvancED Flex 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.