Here is an example code, of how to enable mouse and keyboard interaction within your papervision actionscript file.

I know I was lazy, and have not put any comments in the source code, however if you need any extra help or explanations, please leave a comment, and I WILL get back to you

May promise a better full tutorial later on….

import flash.display.Sprite;
import flash.geom.Point;
//Import Camera, Scene, etc..
import org.papervision3d.view.BasicView;
//Mouse Movements
//rotation calculations
import org.papervision3d.core.math.Matrix3D;
import org.papervision3d.core.math.Number3D;

*@Author: Thierry Zoghbi
*@Date: 2009.

[SWF(width="800", height="600", backgroundColor="#0066F", frameRate="25")]

public class Mainsack extends BasicView

private var container : DisplayObject3D;
private var previousMousePoint : Point = new Point();
private var targetScale : Number = 1;
private static const FORWARD:Number3D = new Number3D(0, 0, 1);

public function Mainsack()
var yourobject = new Collada("yourDAEfile.dae");

this.stage.doubleClickEnabled = true;
this.stage.addEventListener( MouseEvent.DOUBLE_CLICK, onDoubleClick );
this.stage.addEventListener( MouseEvent.MOUSE_DOWN, whenMouseDown );
this.stage.addEventListener( MouseEvent.MOUSE_WHEEL, whenMouseWheel );
this.stage.addEventListener( KeyboardEvent.KEY_DOWN, whenKeyDown );


private function whenKeyDown( event : KeyboardEvent ) : void
trace( event.keyCode );
switch ( event.keyCode )
case 187: //
case 107: //
zoom( 3 );
case 189: // –
case 109: // –
zoom( -3 );
case 82: // r (reset)

private function whenMouseWheel( event : MouseEvent ) : void
zoom( );

private function onDoubleClick( event : MouseEvent ) : void
zoom( 10 );

private function whenMouseDown( event : Event ) : void
previousMousePoint = new Point(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
this.stage.addEventListener( MouseEvent.MOUSE_MOVE, whenMouseMove );
this.stage.addEventListener( MouseEvent.MOUSE_UP, whenMouseUp );

private function whenMouseUp( event : Event ) : void
this.stage.removeEventListener( MouseEvent.MOUSE_MOVE, whenMouseMove );
this.stage.removeEventListener( MouseEvent.MOUSE_UP, whenMouseUp );

private function whenMouseMove( event : Event ) : void
var currentMousePoint:Point = new Point(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);

var difference:Point = currentMousePoint.subtract(previousMousePoint);
var vector:Number3D = new Number3D(difference.x, difference.y, 0);

var rotationAxis:Number3D = Number3D.cross(vector, FORWARD);

var distance:Number = Point.distance(currentMousePoint, previousMousePoint);
var rotationMatrix:Matrix3D = Matrix3D.rotationMatrix(rotationAxis.x, -rotationAxis.y, rotationAxis.z, distance/(600*Math.pow(container.scale, 5)));
container.transform.calculateMultiply3x3(rotationMatrix, container.transform);

//this line used to apply transform to actual rotation values, so that if you change scale, the changes are persisted

previousMousePoint = currentMousePoint

trace( container.rotationX, container.rotationY, container.rotationZ, container.scale );

private function zoom( delta : Number ) : void
targetScale = targetScale (delta * .01);
targetScale = Math.max( targetScale, .5 );
targetScale = Math.min( targetScale, 1.6 );
Tweener.addTween( this, {sceneScale:targetScale, time:1, transition:"easeOutQuart"} )

public function resetView() : void
Tweener.addTween( container, {time:3, rotationX:0, rotationY:0, rotationZ:0, transition:"easeOutQuart"} )
//Tweener.addTween( container, {time:3, rotationX:-45, rotationY:0, rotationZ:0, transition:"easeOutQuart"} )
Tweener.addTween( this, {sceneScale:1, time:3, transition:"easeOutQuart"} )

public function set sceneScale( value : Number ) : void
container.scale = value;

public function get sceneScale() : Number
return container.scale;


Be Sociable, Share!

8 Responses to “Mouse and Keyboard Function in Actionscript3 and Papervision”

  • seanNo Gravatar says:

    I am getting a syntax error on this line:
    container.transform.calculateMultiply3×3(rotationMatrix, container.transform);

    Great tutorials other than that! Thanks

  • adminNo Gravatar says:

    what is the error exactly?

  • seanNo Gravatar says:

    it says

    1084: Syntax error: expecting rightbrace before 3.

  • adminNo Gravatar says:

    make sure you dont have any extra brackets in your code either before or after the line.
    as this sometimes can cause a problem. also make sure you have your semicolons put in the right places!

    the code above should work as i have used it my self, just try double checking your code.

  • seanNo Gravatar says:

    the code i am using IS your code. the only thing i changed was the dae file name. This stuff always happens to me haha!

  • SunNo Gravatar says:

    Same problem I’m having now…

  • JakeNo Gravatar says:

    I have just viewed your live example where you can rotate the dae file, zoom in and out etc and I love it. I want to create a similar thing but I just tried this tutorial and Im getting an error on line 100…

    1084: Syntax error: expecting rightbrace before 3.
    1093: Syntax error.

    Please help me out mate. Thanks

  • adminNo Gravatar says:

    That syntax error is a basic problem with semicolons or other mistypes.
    This is the annoying part in checking and rechecking your code for grammatical errors…

Leave a Reply

This site is using OpenAvatar based on

Follow me on Twitter
free counters
PayPal Donation
If you like this blog and the help it has given you, why not donate for this good cause? Click on the button below to pay Safely and Securely through PayPal.