4. Basic Template Usage
Now that you've created you're base template, you'll probably be eager to try it out.
Let's start by simply creating a rotating cone, much like this one:
By using the base class that you created, you can really quickly build this simple example.First, create a new project in FlashDevelop. Call it "ConeExample" or something similar.Go to "Project > Properties..." and type "cone.swf" into the Output File field. Under the Classpaths tab, you need to add two classpaths this time.Firstly in the classpaths tab, click Add Classpath... Navigate to your GreatWhite source code directory and click OK
Now we need to add our template to the project. Click on "Add Classpath..." again, and this time go into the directory where you saved your base template. This will probably be "My Documents/PaperBase", if you called the project the same name as mine. Find the folder that contains PaperBase.as and click OK.
We now need to create the code.
Right click on "ConeExample" under the project tree, go to Add and choose New Class.
Name the class Main.as.
Right click on Main.as, under ConeExample in the project tree, and click on Always Compile.
You're now ready to start coding.
Firstly, add the following lines to Main.as, under the line "package {"
import PaperBase; import org.papervision3d.objects.primitives.Cone;
Those lines will import all of the packages we need.
Next, change the line:
public class Main {
To
public class Main extends PaperBase {
This will make our project import all of the functions and code that we wrote in PaperBase.as. We can now add just a few lines to make our papervision project work.
We'll need a cone object to add to the scene. Directly underneath the line
public class Main extends PaperBase {
You need to insert the line:
public var cone:Cone = new Cone();
This line will make a new cone object, and hold it in the variable called "cone".
Now, when the project is run, the code in "public function Main() {" is called. We want to initialise our papervision skeleton when our project runs. To do this, change the function so that it looks like this:
public function Main() {
init();
}
This will run the PaperBase init function when your project runs.
You now need to override two functions from PaperBase. These are init3d and processFrame.
We'll need to add our cone to our scene in init3d, then rotate it a bit every time processFrame is called. To do this, add the following code directly below the Main() function.
override protected function init3d():void {
cone.scale = 3; // Make the cone bigger
cone.pitch(-40); // Tilt it towards the camera
default_scene.addChild(cone); // Add it to the scene
}
override protected function processFrame():void {
cone.yaw(5); // Rotate it a bit
}
These two "override" functions will be run instead of the ones in PaperBase. When you run your project you should see the cone rotating! Try playing with the values, adding and changing bits in the two functions above.
The final code should look like this:
-
package {
-
-
import PaperBase;
-
import org.papervision3d.objects.primitives.Cone;
-
-
public class Main extends PaperBase {
-
-
public var cone:Cone = new Cone();
-
-
public function Main() {
-
init();
-
}
-
-
override protected function init3d():void {
-
cone.scale = 3;
-
cone.pitch(-40)
-
default_scene.addChild(cone);
-
}
-
-
override protected function processFrame():void {
-
cone.yaw(7);
-
}
-
-
}
-
-
}




I'm getting the error "Ambiguous reference to Vertex3D". I have the correct reference to the Vertex3D class.
What am I missing?
Hi,
To be honest I haven't got a clue!
Could you post the line of code that it takes you to when you double click the error?
Thanks,
Luke
Hi,
I am using Flash CS3 on a Mac and I successfully (no errors) ported the tutorial to CS3. But: I got nothing to see in the SWF.
Can you give me a hint? Did I miss somthing? Like creating an initial movieclip and name it somehow?
Regards.
Mike
Addition:
This is the output after compilation:
Papervision3D Public Alpha 2.0 - Great White (3.12.07)
PV3D 2.0a WARNING : DO NOT USE WITH BETA 9 PLAYERS. ONLY WITH OFFICIAL TO TEST.
CHECK YOUR VERSION!
DisplayObject3D: null
DisplayObject3D: null
DisplayObject3D: null
DisplayObject3D: vsn-mats
BitmapFileMaterial: Loading bitmap from http://papervision2.com/wp-content/downloads/dae/Cow.png
(Doesn't help, does it?)
Hi Mike!
Ok, the output shows that Papervision 3d is initialising, and working, so that's good.
Two suggestions:
1: When you call init(), pass it the width and height of your movie, so the line might read like this:
(550 is the width, 400 is the height)
2: If that doesn't work then maybe the viewport isn't being added to the stage properly. The code that adds the viewport to the scene is in PaperBase.as, the line:
Try changing that to one of the following:
OR
Tell me if it works!
-Luke
Hi Luke,
at first: I am impressed how fast you're answering. Thanks.
Then: Your suggestion about the root or stage did the trick. But I could not use it directly in the PaperBase class.
I had to push it through all Methods:
cmd action in ConeExample.fla:
With this,the cone example works in Flash CS3 as well. Again: Thanks a lot.
Mike
Hi, Mike.
Did you defined the "Document class" of the FLA file? Because I did tried with the Flash CS3 and it works fine.
Bye,
CaioToOn!
Hi CaioToOn,
with this "trick" it works without passing the root to the methods. I did not know about that, because I am coming from AS2. But after your hint, I red about the "All-is-a-class" - priciple in AS3. And I like it very much, because it is much closer to OOP, then it was before.
Thanks for the hint.
Mike
I am trying to run this from flash and i get this error:
1020: Method marked override must override another method.
1020: Method marked override must override another method.
??
Hi David,
Not sure I can help you there sorry.. If you supply more info about the error I might be able to help.
Thanks for visiting!
-Luke
Hey thanks a ton for the Tutorials, they are awesome. I wanted to see if anyone else is getting the same issue I am having. Everything I am doing with the Papervision3D shows up towards the lower right corner of my screen. I am assuming that following the above example you would have the cone and everything lined up at (0,0,0) origin. Anyone have any ideas - I have a Dell XPS M1730, NVIDIA 8700 running at a resolution of 1920 x 1200 (if this makes any difference). Anyone have this issue? Most of the demos I run seem to layout fine.
Hi Chuck,
Thanks for your comment on my site.
I have an idea what the problem might be.
Try changing the code:
public function Main() {
init();
}
Change that code to:
public function Main() {
init(400,300);
}
Then right click on the project in the project tree and click Properties. In the SWF settings boxes, set the width setting to 400 and height to 300.
I hope this helps!
-Luke
Man ~ you are awesome !
applause!
Thank you for the tutorials and the replies - this is a great resource. Can you show how to build a tile-based world, with a little random vertex displacement, so that it does not look completely flat? Thanks!
I don't really have a clue, but I'm guessing that I'm missing something?
1046: Type was not found or was not a compile-time constant: Viewport3D.
Thanks Mediakid for the kind words!
Alex,
I'll try to put an example together within a few weeks, but It's quite a tall order, will take a lot of time to script something like that!
Michael,
Check that you've imported the papervision library correctly, and the import line for Viewport3D is at the top of your file.
-Luke
@Chuck (or anyone else)
If you compiling your .swf from flash, you need to match your .swf's stage properties to those specified in the PaperBase.as init().
This function sets the width and heights to 800 and 600.
To get around this without breaking encapsulation (as it is) in your main.as change:
to
"1200" and "600" is the width and height of my Fl stage. Change those numbers to your height and width.
Hi
I have a different problem here, I'm sure it's simple
The error is : Error: Access of undefined property scene.
So on this line : scene.addChild(cone);
It just doesn't work...
Sorry,
Problem resolved
Can we go back to the top of this thread in regards to the Vertex 3D error? Was that ever resolved, because I get the same thing.
Hi guys,
I followed up each instruction but I received this error message:
Main.as(11): col: 28 Error: The definition of base class PaperBase was not found.
When I type the code I found the PaperBase class, so I'm not sure why it tells me that
it didn't find it.
Thanks
Nevermind,
I just click on Build Project and it worked.
Regards
Jose Luis
I got nothing to see in the SWF.Can you hel me?
When click on "cone.swf" under ConeExample(AS3) i receive
"Swf error:Unable to read beyond the end of the stream"
Thanks
This is a fantastic resource. Thank you very much.
Thanks for the tutorials, they´ve been very handy
Help FD3 with Papervision3D.
Hi,
I am new to flash development
1.I have downloaded FlashDevelop 3.0.0 Beta 6 , Flex 3 SDK and Papervision 3d 2.0.
Flex SDK Location = C:\FlexSDK\
Installed it successfully.
2.create a "Base Class":
2.1. I started a new as3 EMPTY Project
(Create a new FlashDevelop Project select “ActionScript 3 -> Empty Project)
name :PaperBase,Locatons: ..\My Documents\Papervision
Click on the Classpaths tab and click Add Classpath for Papervision 3d(Browse to the src folder)
2.2 Add > New Class name:PaperBase.as and select "Always Compile".
2.3.PaperBase.as source code :
package {
import flash.display.Sprite;
import flash.events.Event;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.cameras.*;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.render.BasicRenderEngine;
public class PaperBase extends Sprite {
public var viewport:Viewport3D;
public var renderer:BasicRenderEngine;
public var default_scene:Scene3D;
public var default_camera:Camera3D;
public function init(vpWidth:Number = 800, vpHeight:Number = 600):void {
initPapervision(vpWidth, vpHeight);
init3d();
init2d();
initEvents();
}
protected function initPapervision(vpWidth:Number, vpHeight:Number):void {
viewport = new Viewport3D(vpWidth, vpHeight);
addChild(viewport);
renderer = new BasicRenderEngine();
default_scene = new Scene3D();
default_camera = new Camera3D();
}
protected function init3d():void {
}
protected function init2d():void {
}
protected function initEvents():void {
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
protected function processFrame():void {
}
protected function onEnterFrame( ThisEvent:Event ):void {
processFrame();
renderer.renderScene(default_scene, default_camera, viewport);
}
}
}
3.Create a new FlashDevelop Project ( select “ActionScript 3 -> Empty Project) :name:"ConeExample" ,
Locatons: ..\My Documents\,
type "cone.swf" into the Output Fil and add Classpath for PaperBase and Papervision 3d.
Right click on "ConeExample" under the project tree, go to Add and choose New Class.
Name the class Main.as.
Right click on Main.as, under ConeExample in the project tree, and click on Always Compile.
3.1 Main.as:
package {
import PaperBase;
import org.papervision3d.objects.primitives.Cone;
public class Main extends PaperBase {
public var cone:Cone = new Cone();
public function Main() {
init();
}
override protected function init3d():void {
cone.scale = 3;
cone.pitch(-40)
default_scene.addChild(cone);
}
override protected function processFrame():void {
cone.yaw(7);
}
}
}
I see white background. Nothing on the screen.
Can anyone help me?
Hi,
thx for the tutorial but i can't make it works! cone.swf is empty(white), i've followed each step but it doesn't work...
)
no error at compilation, just an error on the cone.swf : Impossible to read beyond the stream's end(a bad traduction from french
I couldnt get this to work using just flash develop, but using the IDE it does work without error. The error seems to be that flash develop doesnt see the papervision properly or half sees it as it does seem to import the class ???
here is the first of similar (same cause) error
C:\work\as3\ConeExample\PaperBase.as(20): col: 23 Error: Type was not found or was not a compile-time constant: BasicRenderEngine.
What I did differently was have my document Main.as AND PaperBase.as in the project folder. I dont see the problem as it all compiles but something IS wrong.
About "Ambiguous reference to Vertex3D". try to remove olders versions of Papervision classes files that you have in your classes path.
When I test the project I can't see anything in the swf. This is the message that returns FlashDeveloper:
Waiting for Player to connect
Failed to connect; session timed out.
Ensure that:
1. you compiled your Flash movie with debugging on, and
2. you are running the Debugger version of the Flash Player.
hi there! thank you luke for all your information.
one quick note to everybody who came here after playing with pv3d before and tried the tutorials from luke (and what to try everything in flash cs3,too):
1. the flashdevelop + flex sdk is cool!
2. everything works in flash cs3, too!
- here comes my note:
3. delete the links to papervision classes in your flash cs3 preferences! use only the links to papervision and paperbase from luke in the "publish" settings of your cs3 document and link the main.as to the document class.
have fun!
chem!
Hi.
These are some great tutorials, the best I have found on using Papervision3d. I am new to the Flash Developer but it seems pretty simple to use. My question is after I put in all the code for the Cone.as what do I do to use it or display anything? I have tried to build and execute the file after putting in all of the code but keep coming up with this error:
C:\ppv3d\ConeExample/Main.as:7: characters 0-7 : parse error Unexpected package
The package it is referring to is the package created in the fourth tutorial, this tutorial. I am just wondering if anyone knows what the issue is or if I am doing anything wrong or perhaps leaving some steps out.
Feel free to ask questions to help better understand my situation as I would be greatly appreciative of anything that can help me with this issue.
Here is the code i have from tutorial four.
/**
* ...
* @author Default
* @version 0.1
*/
package{
import PaperBase;
import org.papervision3d.objects.primitives.Cone;
public class Main extends PaperBase {
public var cone:Cone = new Cone();
public function Main() {
init();
}
override protected function init3d():Void {
cone.scale = 3; // Make the coen bigger
cone.pitch( -40); // Tilt it towards the camera
default_scene.addChild(cone); // Add it to the scene
}
override protected function processFrame():Void {
cone.yaw(5); // Rotate it a bit
}
}
Hi,
I'm just starting out using PV3D and I cant get your examples to run.
I have great white "org" folder in the same folder as my project.
I get an error saying 1020: Method marked override must override another method.
Can you help?
I was also having the blank screen problem using Flex. I spent some time tracking down the problem and found a solution. I created a new Flex project and made the mxml file look like this:
If your Main class is not in the source root folder you'll have add an import for it.
Sorry the comment box ate the XML, I'll try again:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml "
layout="absolute"
creationComplete="complete()"
>
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
public function complete() : void
{
var sprite:Main = new Main();
var component:UIComponent = new UIComponent();
component.addChild(sprite);
addChild(component);
}
]]>
</mx:Script>
</mx:Application>
Hello, I got this message " TriangleMesh 1020: Method marked override must override another method ". I'm sure that I did the installation and the pathing stuff correct, anyone can help me. none of the examples runs, there always appears the same error.
ThankU
hi!
i did everthing like in this tut, i think, and my cone is displayed and rotating but it is not rotating cleanly. it is some kind of wiggling while rotating!
but why?
here is my example: http://www.pixelfiction.de/pv/cone.html
this sux!
Hi, im just learning pv3d and these tutorials are great, although i have tried the Cone Example and the Complex Cow Model tutorials and both of them are not rotating smoothly as in the examples, the just randomly jump and spin around.
Can anyone help?
something changed in Great White in the last 2 months.
this example does not work as shown any more.
it DID work 2 months ago. ~ March 15
In DisplayObject3d.as, the directional vectors are not being cloned during yaw or roll. The object is losing it's sense of direction.
Thanks for letting me know, was driving me crazy!
@pester - I had the same issue. The repository has since been updated. Try downloading the src again and the problem should go away :]
I did this again and again, and it says package is unexpectable, omg! y this happens to me
i'm getting this actually.,
col: 7 Error: Syntax error: package is unexpected.
package {
^
Build halted with errors (mxmlc).
Done (1)
i think Build halted with errors means the two class paths are overlapping, isnt it? Plz help!
I'm using the PaperBase.as class with FlexBuilder3 and GreatWhite.
Here is how I figured out how to use it.
First thing I did was to CREATE A NEW folder(PaperBase) in the same directory as where I have the Papervision3d classes. ex. ..\org\papervision3d\PaperBase
Copy/paste the file PaperBase.as into the new folder.
Start Flex
In flexbuilder: File->New->Actionscript project
Chose a name like Papertutorial for the project and CLICK 'NEXT'
in the 'Source path' tab I click 'Add Folder' and browse to the ..\papervision3d folder and click 'Accept/OK' to add the folder.
I usually create a separate 'Main source folder' for the project in the browse tab.
Click 'Finish' to start building the project setup.
I create a Main.as: File -> New-> Actionsriptclass
Set the name to Main.as and the Superclass to PaperBase and click 'Finish' to create the file.
In you project class(PaperTutorial.as) you need to create and add an instance of the main in the constructor.
public function PaperTutorial()
{
var main:Main = new Main();
addChild(main);
}
Now you should be able to write/copy/paste the examples from this tutorial into your Main.as and run the examples.
Forgot to tell on IMPORTANT CHANGE!
You need to change the package declaration in the PaperBase.as to
package org.papervision3d.PaperBase{
and you need to include it in the Main.as
package
{
import org.papervision3d.PaperBase.PaperBase;
public class Main extends PaperBase
{
public function Main()
{
super();
}
}
}
I've been using this base class or a while but I'm still not sure if I'm implementing it properly when working with a larger project. It works fine when working with one class, but when I additionally extend this class to another Papervison3D class in the same SWF there seems to be a performance issue. I'm convinced it has something to do with viewport and not having set the stage property in PaperBase. If I try to define the stage from the Document Class and call that class in PaperBase I get both 3D objects appearing at the same time on the stage. But if I leave PaperBase alone to find it's own display property performance is effected, more noticeably if there are even more than two 3D classes in the project. It seems to me like the viewport is not being removed from the display list each time class is called to be removed but I can't figure out how to prove it. Does anyone know how this should be set-up?
Thanx you very much
finally,
my cube rotate in mad direction!!!
override protected function init3d():void {
cone.scale = 3;
cone.pitch(-16)
default_scene.addChild(cone);
cone.x = 100; cone.y = 700;
addEventListener(Event.ENTER_FRAME, prova);
}
private function prova(ev){
//cone.x = cone.x + 8;
if (r<20){r++;}else {r=-30;};
cone.pitch(r);
}
@xcorrect - I bet you just copied and pasted the example text! - but you didn't read "View as Plain Text". I did the same! but recopying at just plain text makes it work...
great tutorials btw.
I'm getting the
1046: Type was not found or was not a compile-time constant: Viewport3D.
Hello.
Firts of all i would like to thanks to the author of this website.
You do a great job here
Your scripts working (today)
very good(flash CS3 - but the file Paper Base i have in the same folder of my project).
Yesterday i had a problem with them and i saw a blank swf file. I was so angry. Then all examples from papaervision example folder were blank after compilation so I...reinstal the flash. Now there is no problem with the compilation.
Yours and papervision examples are working fine. This is not the best solution but works
I do not know why the problem occur but i'm happy that is gone.
Thank you Luke for your work. I'm waiting for new posts and tips from you about Papervision
hullo. I also get:
Vertices3D.as, Line 163: 1000: Ambiguous reference to Vertex3D. vertex : Vertex3D,
Vertices3D.as, Line 222: 1000: Ambiguous reference to Vertex3D. for each(var v:Vertex3D in vertices)
Bug in GreatWhite? I'm running in CS3 on Mac
Hokusai_spain is correct. I removed the references to previous versions of Papervision and the issue was resolved.
I'm experiencing some strange behavior using the examples in Flex 3.
In my PaperBase class, the default width (vpwidth) is 800 and the default height (vpheight) is 600.
In my ConeExample class, I set both the width and height to 100. Yet when I test the app, the display area is actually 238x238.
Why is that?
I have one other problem. When i use this code, i do get result only in FlashDevelop when i press build or test. When i move this code to my Apache web server and load the page in web browser then, all i see is this:
http://hq.mednmleko.com:8080/papervision2/
I was trying to edit papervision2Config.xml to suit my needs and still I cannot set the relative path for those libraries mentioned in this config file :(( Can anyone give me any suggestion, how do we export or build the code, so it could be used on the web site.
I get this error
C:\..\My Documents\spinningCone\Main.as(14): col: 11 Error: The protected attribute can only be used on class property definitions. in the RESULTS and the OUTPUT.
Then I copied and pasted the code found here but I still get the same error.
Anybody know what could be the problem?
I don't know where to start looking.
I'm using FD3 and Papervision 2
Here is my code for this tut
//
package {
import PaperBase;
import org.papervision3d.objects.primitives.Cone;
public class Main extends PaperBase {
public var cone = new Cone();
}
public function Main() {
init();
}
override protected function init3d():void {
cone.scale = 3; //makes cone bigger
cone.pitch( -40) //tilt it towards the camera
default_scene.addChild(cone); //add the cone to the scene
}
override protected function processFrame():void {
cone.yaw(5); //rotation
}
}
//
Thanx
nevermind...blind and stupid me missed the one '}' after I declare the cone var. Shouldn't be there. Will teach me to go over my code one more time before posting anything.
Hey there!
everything seems to work well, apart from calling the yaw() an pitch() methods, and i cannot quit figure out what is going wrong or what i am doing wrong. Annyone an idea?
This is the error i get..
1195: Attempted access of inaccessible method pitch through a reference with static type org.papervision3d.objects.primitives:Cone.