The Simplest Way to Assign a PespectiveProjection Instance to a Display Object

Th applet below does not differ in functionality from the applet on page 1. The code behind it is a bit different, though. We do not have the Sprites container1 and container2 anymore. holder1 and holder2 are children of the MainTimeline. PespectiveProjection instances pp1 and pp2 are assigned to holder1 and holder2.

The most important observation in this experiment is the following. Since pp1 and pp2 are assigned to 3D objects holder1 and holder2, the coordinates passed to pp1.projectionCenter and pp2.projectionCenter are relative to the first 2D parent, the MainTimeline. Since we want the center of each tile to be the projectionCenter, we pass the stage coordinates of the centers of the upper and the lower tiles to the pp1.projectionCenter and pp2.projectionCenter, respectively.

The Code

Below is the code in its entirety. The file in the pp.zip corresponding to thes experiment is pp_show4.fla.

var picWidth:Number=144;

var picHeight:Number=108;

var img1:Bitmap=new Bitmap(new Pic144(144,108));

var img0:Bitmap=new Bitmap(new Pic144(144,108));

var img2:Bitmap=new Bitmap(new Pic144(144,108));

var holder1:Sprite=new Sprite();

var holder0:Sprite=new Sprite();

var holder2:Sprite=new Sprite();

this.addChild(holder1);

this.addChild(holder0);

this.addChild(holder2);

holder1.x=100;

holder1.y=120;

holder0.x=300;

holder0.y=250;

holder2.x=500;

holder2.y=390;

holder1.addChild(img1);

img1.x=-picWidth/2;

img1.y=-picHeight/2;

holder0.addChild(img0);

img0.x=-picWidth/2;

img0.y=-picHeight/2;

holder2.addChild(img2);

img2.x=-picWidth/2;

img2.y=-picHeight/2;

var pp1:PerspectiveProjection=new PerspectiveProjection();

pp1.fieldOfView=100;

pp1.projectionCenter=new Point(holder1.x,holder1.y);

var pp2:PerspectiveProjection=new PerspectiveProjection();

pp2.fieldOfView=100;

pp2.projectionCenter=new Point(holder2.x,holder2.y);

holder1.transform.perspectiveProjection=pp1;

holder2.transform.perspectiveProjection=pp2;

this.transform.perspectiveProjection.fieldOfView=100;

 

function spinImgs(e:Event):void {

holder1.rotationY+=2;

holder0.rotationY+=2;

holder2.rotationY+=2;

}

 

btnStart.addEventListener(MouseEvent.CLICK,btnStartClick);

 

function btnStartClick(e:MouseEvent):void {

this.addEventListener(Event.ENTER_FRAME, spinImgs);

}

 

btnStop.addEventListener(MouseEvent.CLICK,btnStopClick);

 

function btnStopClick(e:MouseEvent):void {

this.removeEventListener(Event.ENTER_FRAME, spinImgs);

}

 

btnRemove.addEventListener(MouseEvent.CLICK,btnRemoveClick);

 

function btnRemoveClick(e:MouseEvent):void {

holder1.transform.perspectiveProjection=null;

holder2.transform.perspectiveProjection=null;

}

 

btnRestore.addEventListener(MouseEvent.CLICK,btnRestoreClick);

 

function btnRestoreClick(e:MouseEvent):void {

holder1.transform.perspectiveProjection=pp1;

holder2.transform.perspectiveProjection=pp2;

}

Download

  • Download all the source files corresponding to this tutorial: pp.zip

Back to Flash CS4 Tutorials              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.