What is BasicView?

Saturday, December 6th, 2008 | how it works, tutorials

I use BasicView in all of my examples. This post explains why:


1. BasicView is a Sprite

Every ActionScript 3 project requires a “document class“. Since BasicView subclasses Sprite, you can extend BasicView to create your document class:

public class MyAwesomeDocumentClass extends BasicView

*note – You’ve probably heard the terms subclass, inherits, inheritance, extends, or others before. All those terms just mean you take advantage of the variables and functions of a specific class by writing another class that extends it. See Wikipedia’s Inheritance Page.

If you look at the docs, you’ll see the inheritance chain: BasicView->AbstractView->Sprite.


2. BasicView provides a viewport, scene, camera, and renderer

Every Papervision3D project requires a viewport, scene, camera, and renderer which are all classes in the Papervision3D library. If you don’t use BasicView, you have to do something like this:

package
{
	import flash.display.Sprite;
	import flash.events.Event;
 
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class ImTooCoolForBasicView extends Sprite
	{
		private var viewport:Viewport3D;
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var renderer:BasicRenderEngine;
 
		public function ImTooCoolForBasicView()
		{
			viewport = new Viewport3D();
			addChild(viewport);
 
			scene = new Scene3D();
			camera = new Camera3D();
			renderer = new BasicRenderEngine();
 
			var sphere:Sphere = new Sphere();
			scene.addChild(sphere);
 
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
 
		private function enterFrameHandler(event:Event):void
		{
			renderer.renderScene(scene, camera, viewport);
		}
 
	}
}

Phew, that was a lot of work! Let’s look at what it looks like when you use BasicView:

package
{
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class SmartGuysUseBasicView extends BasicView
	{
		public function SmartGuysUseBasicView()
		{
			var sphere:Sphere = new Sphere();
			scene.addChild(sphere);
			startRendering();
		}
 
	}
}

Much better!

You probably also noticed that the second example has the handy little “startRendering()” function. StartRendering does the exact same thing as the addEventListener(Event.ENTER_FRAME, enterFrameHandler); line in the first example. The addEventListener line is hidden away in AbstractView:

http://code.google.com/p/papervision3d/source/browse/trunk/as3/trunk/src/org/papervision3d/view/AbstractView.as#29

Hiding functionality like that example is called encapsulation, but who really cares what it’s called as long as it makes it easier :)


3. What’s this override protected function onRenderTick thing in the examples?

onRenderTick is the enter frame event listener called when you use startRendering(). If you look at the code, you’ll notice onRenderTick is a protected function. Using protected allows you to change what the function does by overriding it.

*note – The keyword protected is one of ActionScript 3’s “accessibility modifiers”. You may be familiar with other keywords: public, private, internal, and final. To learn how each of these “control access” to functions, read through adobe’s keyword page: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html

By default, you just type startRendering(); and onRenderTick will just do the following each frame (you don’t have to type this out since it’s included in AbstractView):

renderer.renderScene(scene, camera, viewport);

You can change what onRenderTick does by writing it out like this:

override protected function onRenderTick(event:Event=null):void
{
	sphere.yaw(1);
	renderer.renderScene(scene, camera, viewport);
}

When overriding a function, remember that the parameters (event:Event=null) and the return type (void) have to remain exactly the same.


4. I get it! BasicView simply sets up what you need for Papervision3D!

Bingo! But that doesn’t mean you have to use BasicView. In fact, maybe your project requires multiple viewports or cameras and you’ll need to write out the code without BasicView. In the end, use what’s best for you.

As for me and my examples, I’ll be sticking with BasicView. :D

*final note – If you found how BasicView works interesting, I recommend reading up on the Template Pattern and the Facade Pattern.

Tags:

  • As
    assasa
  • I was wondering how u can adjust the way that basic view is added to its parent container. It seems to always center itself within the middle of the parent container. I can manually reset its x,y coords but they are relative to the position it first gets added. is there way were addedChild.y=0 can actually be parents.y=0 as well?
  • TimQ
    Thanks so much for that. I'm just starting out with papervision and was totally confused as some examples show BasicView and some didn't. Now I know why!
  • Chris
    This was a very funny and useful tutorial to get me started! Thanks!
  • I seem to be doing something stupid - whenever I try this example I get an error - "5008: The name of definition 'Trackball' does not reflect the location of this file. Please change the definition's name inside this file, or rename the file. C:\Papervision\LatestTrunkfiles\as3\examples\FlashCS3\helloMouse3D\Main1.as"

    I must be stupid 'cos I can't see what's wrong. Any clues?
  • You can also use singleRender() when you use BasicView, instead of renderer.render(a,b,c). Makes the onRenderTick code cleaner again.
  • toffee
    Can you explain, how to setting if I change the basic render to QuadrantRenderEngine.
  • Doh! I just needed to import: flash.events.Event; I knew it was something dumb.
  • John Lindquist
    @Thomas- Just tested 839. Works fine for me.
  • John Lindquist
    Hey Paul, I'm pretty sure that means your braces aren't lined up somewhere.
  • Thomas W
    Hi, anyone ran this example with SVN 839? I keep getting error when trying to create a sphere:

    54:coerce org.papervision3d.core.geom::Vertex3D
    VerifyError: Error #1014: Class org.papervision3d.core.geom::Vertex3D could not be found.
    at org.papervision3d.objects.primitives::Sphere/::buildSphere()
    at org.papervision3d.objects.primitives::Sphere$iinit()
    at PV3DTest4$iinit()
    at test_v03_fla::MainTimeline/test_v03_fla::frame1()

    Vertex3D moved down a folder, al classes I checked referred to the right version...

    Best,

    Thomas
  • I'm getting an error on the override protected function: "The protected attribute can only be used on class property definitions."
    Perhaps a typo on the parameters you have posted here?
blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

December 2008
M T W T F S S
« Nov   Jan »
1234567
891011121314
15161718192021
22232425262728
293031  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...