3. Creating a Papervision Base Template

**UPDATE**
Creating and using this custom template is no longer needed. Papervision now includes a BasicView.as see updated tutorial:
Getting started using BasicView.as
view-update

The first thing that you should do when creating a Papervision 3D project is to create a "Base Class" which will serve as a template to speed up the start of each papervision project you attempt.

1) Create a new FlashDevelop Project

Open up FlashDevelop. On the start page, click on the New: Project... Under the Recent Projects Window.

New Project....

The New Project dialog box will appear. Under Actionscript 3, click Empty Project. Type a name into the Name textbox, and tick the box labelled "Create Directory for Project".

New Project Dialog
(Click To Enlarge)

Click OK to close the dialog box. You will now see your project under the Project window. We need to add a source file to the project.

The first thing you need to do to your project is to import the papervision source so that your program can use it. From the menu at the top, choose Project, then Settings.

The Project settings dialog box will appear. Click on the Classpaths tab and click Add Classpath.

You now need to browse to the folder in which you downloaded the GreatWhite source code in tutorial number 2. Select the greatwhite folder (It should contain directories called org, fl and com) and click OK.

You should now have an icon which looks like a Lego Brick containing the GreatWhite source code under your project.

Project Tree

You now need to make a script to be executed when your program runs. Right click on your project in the project tree and goto Add > New Class.

Add New Class

Name the new class PaperBase.as and click OK. You will now have the Main.as file open in your editor. The code will look like this:

Actionscript:
  1. /**
  2. * ...
  3. * @author Default
  4. * @version 0.1
  5. */
  6.  
  7. package {
  8.    
  9.     public class PaperBase {
  10.        
  11.         public function PaperBase() {
  12.            
  13.         }
  14.        
  15.     }
  16.    
  17. }

This is a blank class. Firstly, we need to make this class compile when we run the project. To do this, right click on Main.as under the project tree and select "Always Compile".

We also need to specify the output SWF location. To do this, from the Project menu, select Properties.

On the Output tab under General, you'll see an input box labeled "Output File". Click Browse and choose where to place the output file.

Now, change the source code so that it looks this (Comments in the code explain each step of the way):

Actionscript:
  1. package  {
  2.     // These lines make differant 'pieces' available in your code.
  3.     import flash.display.Sprite; // To extend this class
  4.     import flash.events.Event; // To work out when a frame is entered.
  5.    
  6.     import org.papervision3d.view.Viewport3D; // We need a viewport
  7.     import org.papervision3d.cameras.*; // Import all types of camera
  8.     import org.papervision3d.scenes.Scene3D; // We'll need at least one scene
  9.     import org.papervision3d.render.BasicRenderEngine; // And we need a renderer
  10.    
  11.     public class PaperBase extends Sprite { //Must be "extends Sprite"
  12.        
  13.         public var viewport:Viewport3D; // The Viewport
  14.         public var renderer:BasicRenderEngine; // Rendering engine
  15.         // -- Scenes -- //
  16.         public var default_scene:Scene3D; // A Scene
  17.         // -- Cameras --//
  18.         public var default_camera:Camera3D; // A Camera
  19.        
  20.         public function init(vpWidth:Number = 800, vpHeight:Number = 600):void {
  21.             initPapervision(vpWidth, vpHeight); // Initialise papervision
  22.             init3d(); // Initialise the 3d stuff..
  23.             init2d(); // Initialise the interface..
  24.             initEvents(); // Set up any event listeners..
  25.         }
  26.        
  27.         protected function initPapervision(vpWidth:Number, vpHeight:Number):void {
  28.             // Here is where we initialise everything we need to
  29.             // render a papervision scene.
  30.             viewport = new Viewport3D(vpWidth, vpHeight);
  31.             // The viewport is the object added to the flash scene.
  32.             // You 'look at' the papervision scene through the viewport
  33.             // window, which is placed on the flash stage.
  34.             addChild(viewport); // Add the viewport to the stage.
  35.             // Initialise the rendering engine.
  36.             renderer = new BasicRenderEngine();
  37.             // -- Initialise the Scenes -- //
  38.             default_scene = new Scene3D();
  39.             // -- Initialise the Cameras -- //
  40.             default_camera = new Camera3D(); // The argument passed to the camera
  41.             // is the object that it should look at. I've passed the scene object
  42.             // so that the camera is always pointing at the centre of the scene.
  43.         }
  44.        
  45.         protected function init3d():void {
  46.             // This function should hold all of the stages needed
  47.             // to initialise everything used for papervision.
  48.             // Models, materials, cameras etc.
  49.         }
  50.        
  51.         protected function init2d():void {
  52.             // This function should create all of the 2d items
  53.             // that will be overlayed on your papervision project.
  54.             // User interfaces, Heads up displays etc.
  55.         }
  56.        
  57.         protected function initEvents():void {
  58.             // This function makes the onFrame function get called for
  59.             // every frame.
  60.             addEventListener(Event.ENTER_FRAME, onEnterFrame);
  61.             // This line of code makes the onEnterFrame function get
  62.             // called when every frame is entered.
  63.         }
  64.        
  65.         protected function processFrame():void {
  66.             // Process any movement or animation here.
  67.         }
  68.        
  69.         protected function onEnterFrame( ThisEvent:Event ):void {
  70.             //We need to render the scene and update anything here.
  71.             processFrame();
  72.             renderer.renderScene(default_scene, default_camera, viewport);
  73.         }
  74.        
  75.     }
  76.    
  77. }

This is heavily commented, so it should explain everything that's happening.

When you run the project, you'll just see a blank screen, this is expected because you haven't added anything to the scene yet.

You can download the source file with the code comments or without using the links below:

Download Now

This file will now be used as the basis of any papervision projects that you do, and with very few lines added to that code, you can get really good results.

*EDIT*

Updated 20th August 2008, now works with the GreatWhite codebase changes

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

70 Responses to “3. Creating a Papervision Base Template”


  • Sorry – looks like it was finding my old papervision which is odd because I put in a class path that was higher up… now getting a viewport error – will look into that – saw mention of it up above somewhere…

  • Running process: C:\Documents and Settings\cbinion\Local Settings\Application Data\FlashDevelop\Tools\fdbuild\fdbuild.exe “C:\Documents and Settings\cbinion\My Documents\Testing\Testing.as3proj” -ipc e4deff2b-ca3a-485a-badc-d7d90d960ade -compiler “C:\Documents and Settings\cbinion\Desktop\Tests\Flex” -library “C:\Documents and Settings\cbinion\Local Settings\Application Data\FlashDevelop\Library” -cp “C:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\ActionScript 3.0\Classes” -cp “C:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\Component Source\ActionScript 3.0\User Interface”
    HINT: Improve your build speed when compiling in FlashDevelop3 by placing the Adobe Flex Compiler Shell in your Flex2 bin path.
    Building Testing
    mxmlc -load-config+=obj\TestingConfig.xml -debug=true -benchmark=false -o obj\Testing633608855776912593
    Loading configuration file C:\Documents and Settings\cbinion\Desktop\Tests\Flex\frameworks\flex-config.xml
    Loading configuration file C:\Documents and Settings\cbinion\My Documents\Testing\obj\TestingConfig.xml
    C:\Documents and Settings\cbinion\My Documents\Testing\PaperBase.as(2): col: 7 Error: Syntax error: package is unexpected.

    package {
    ^

    Build halted with errors (mxmlc).
    Done (1)

    So what does all this mean?

  • Hi, first up, ace tutorials – wish all tuts were as good as this.

    OK now for my questions!
    1. I downloaded the source by following the link at the top of Tutorial 2 but I only have two folders: org and nochump instead of org com and fla. Is this right? Will it still work?

    2. When you specify the output file, does it matter what I call it?

    3.I don’t get main.as when I add a new class, I just get PaperBase.as. I assume this is the same, right?

    I tried to run it and I got a whole mess of errors, so I’m guessing that I have a problem with one of the above.

    Thanks for the great site,

    Wilbo

  • Should I still be getting the Greatwhite thing?

  • It’s better to use this class or to extend BasicView?

  • Hi,

    I’ve been trying to get into Papervision for a while now, and this site is just what I was looking for. These tuts are just awesome. Thanks Luke! But I keep getting 2 errors whenever I compile a project.

    This is what the output says:

    [path\to\]AbstractLightShadeMaterial.as(34): col: 12 Error: Implicit coercion of a value of type String to an unrelated type org.papervision3d.objects:DisplayObject3D.

    [path\to\]CompositeMaterial.as(32): col: 12 Error: Implicit coercion of a value of type String to an unrelated type org.papervision3d.objects:DisplayObject3D.

    I get the same output in Flash CS3.

    I downloaded the last version of Papervision. I was getting this error before, with version 1.5, and upgraded, but with no success. I’m running it on Win XP in a 2Quad CPU Q6600 @ 2.41GHz with 4GB of RAM

    Any clues on what the heck is going on?

  • Hello
    I do not understand these lines

    default_camera = new Camera3D(); // The argument passed to the camera
    // is the object that it should look at. I’ve passed the scene object
    // so that the camera is always pointing at the centre of the scene.

    do you mean in future code you pass this
    maybe like
    default_camera (object to point at)

    sorry is this is a dumb question
    but I am a newbie
    your tut’s rock

  • Great Tutorials!

    I’m very new to papervision and am having trouble getting the blank example to run. this is what is coming up in the output -

    Running process: C:\Program Files\FlashDevelop\Tools\fdbuild\fdbuild.exe “C:\Documents and Settings\tony.dimovski\Desktop\other\PaperBase\PaperBase.as3proj” -ipc bcf670df-9298-4ee6-89ba-7a5f19e02068 -compiler “C:\Documents and Settings\tony.dimovski\Desktop\other\flex2″ -library “C:\Program Files\FlashDevelop\Library”
    Building PaperBase
    mxmlc -load-config+=obj\PaperBaseConfig.xml -debug=true -benchmark=false -o obj\PaperBase633696461124531250
    Exception: The system cannot find the file specified
    Done (1)

    Any help would be VERY VERY appreciated.

    Thanks

  • Let me preface this with “I am new to this and annoying”. Now, I am having a slew of error messages related to type not count or call to a possibly undefined method. Any ideas?

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(20): col: 23 Error: Type was not found or was not a compile-time constant: Viewport3D.

    public var viewport:Viewport3D; // The Viewport
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(21): col: 23 Error: Type was not found or was not a compile-time constant: BasicRenderEngine.

    public var renderer:BasicRenderEngine; // Rendering engine
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(23): col: 28 Error: Type was not found or was not a compile-time constant: Scene3D.

    public var current_scene:Scene3D;
    ^

    C:\Documents and Settings\Tyler Malin\My Documents\paperbase\PaperBase.as(24): col: 29 Error: Type was not found or was not a compile-time constant: CameraObject3D.

    public var current_camera:CameraObject3D;
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(25): col: 31 Error: Type was not found or was not a compile-time constant: Viewport3D.

    public var current_viewport:Viewport3D;
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(27): col: 28 Error: Type was not found or was not a compile-time constant: Scene3D.

    public var default_scene:Scene3D; // A Scene
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(29): col: 29 Error: Type was not found or was not a compile-time constant: Camera3D.

    public var default_camera:Camera3D; // A Camera
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(42): col: 20 Error: Call to a possibly undefined method Viewport3D.

    viewport = new Viewport3D(stage.width, stage.height, true, true);
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(44): col: 20 Error: Call to a possibly undefined method Viewport3D.

    viewport = new Viewport3D(vpWidth, vpHeight, false, true);
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(51): col: 19 Error: Call to a possibly undefined method BasicRenderEngine.

    renderer = new BasicRenderEngine();
    ^

    C:\Documents and Settings\Tyler Malin\My Documents\paperbase\PaperBase.as(53): col: 24 Error: Call to a possibly undefined method Scene3D.

    default_scene = new Scene3D();
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(55): col: 25 Error: Call to a possibly undefined method Camera3D.

    default_camera = new Camera3D();
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(11): col: 38 Error: Definition org.papervision3d.core.proto:CameraObject3D could not be found.

    import org.papervision3d.core.proto.CameraObject3D;
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(13): col: 32 Error: Definition org.papervision3d.view:Viewport3D could not be found.

    import org.papervision3d.view.Viewport3D; // We need a viewport
    ^

    C:\Documents and Settings\Tyler Malin\My Documents\paperbase\PaperBase.as(14): col: 35 Error: Definition org.papervision3d.cameras could not be found.

    import org.papervision3d.cameras.*; // Import all types of camera
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(15): col: 34 Error: Definition org.papervision3d.scenes:Scene3D could not be found.

    import org.papervision3d.scenes.Scene3D; // We’ll need at least one scene
    ^

    C:\Documents and Settings\ \My Documents\paperbase\PaperBase.as(16): col: 34 Error: Definition org.papervision3d.render:BasicRenderEngine could not be found.

    import org.papervision3d.render.BasicRenderEngine; // And we need a renderer
    ^

    Build halted with errors (mxmlc).
    Done (1)

  • Newbie T, got the same problem, right click on your project (“PaperBase”)
    >classpaths, and add path to your papervision code (folder where you have “nochump” and “org”)

    g

  • Excuse me, I find that this example will also work if do not add the parentMovie. We can interact with material directly. So why do we need this hack tech to do the interaction. Is there any advantage?

  • works a charm if you follow the tutorials step by step. Cheers

  • what does that means?

    Error: A file found in a source-path ‘paperBase’ must have the same name as the class definition inside the file ‘PaperBase’.
    Build halted with errors (fcsh).

  • i’m having the same problem of the Tony D.
    Is showing the error:
    ‘Exception: The system cannot find the file specified’

    Thanks!

  • I solved the problem. Just need install de virtual machine java and it works ;D.

  • The first time i tried this things failed.

    turned out i had select ‘Empty project’ under AS2 instead of AS3.

    Silly mistake, but hopefully I’ve saved someone else from repeating it.

  • Hi, i like the intention of this tut., getting anyone started from scratch, except maybe it needs a little update ? I don’t think I am doing anything wrong (hopefully i really don’t) but It does not compile. These are the errors I get:

    E:\AS3\org\papervision3d\objects\DisplayObject3D.as: Error: Can not resolve a multiname reference unambiguously. Papervision3D (from E:\AS3\org\papervision3d\Papervision3D.as) and org.papervision3d:Papervision3D (from E:\AS3\org\papervision3d\Papervision3D.as) are available.

    E:\AS3\org\papervision3d\core\proto\CameraObject3D.as: Error: Can not resolve a multiname reference unambiguously. Papervision3D (from E:\AS3\org\papervision3d\Papervision3D.as) and org.papervision3d:Papervision3D (from E:\AS3\org\papervision3d\Papervision3D.as) are available.

    E:\AS3\org\papervision3d\Papervision3D.as: Error: A file found in a source-path must have the same package structure ”, as the definition’s package, ‘org.papervision3d’.
    Reason: Its dependent file

    I have not changed anything in the provided code, I tried both the most current version of PV3D (Papervision3D_2.1.920) and the the one from the link in the tut2. Both result in the same thing..

    So is it me or is the tutorial outdated and should a change something somewhere ?

    thank you for your effort !

    best regards !

  • thank god. I got blank swf without any errors.

  • How come you have got no constructor in your paperbase class

Leave a Reply


Follow papervision2 on Twitter

RSS Feed