Moving 3D Solids

Moving one of the 3D solids youve created is really no different than moving a 3D movie clip around. You just need to change the center point that the form is based on. Originally, it will be placed at 0, 0, 0. Youve already moved it on the z axis with the zOffset variable. Lets use the keyboard to dynamically change its position on the x and z axes.

First youll add a variable for the xOffset :

 var xOffset:Number = 0; 

In the onEnterFrame function, youll check for any of the cursor keys and adjust the xOffset or zOffset as needed:

 if(Key.isDown(Key.LEFT)) {       xOffset -= 10; } else if(Key.isDown(Key.RIGHT)) {       xOffset += 10; } if(Key.isDown(Key.UP)) {       zOffset += 10; } else if(Key.isDown(Key.DOWN)) {       zOffset -= 10; } 

Youve already dealt with the zOffset when determining scale:

 var scale:Number = fl / (fl + point.z + zOffset); 

Now you add xOffset to point.x , just before you calculate the screen position:

 point.xPos = vpX + (point.x + xOffset) * scale; 

Heres where you are with this, as seen in ch16_13.fla :

 var points:Array = new Array(); var triangles:Array = new Array(); var fl:Number = 250; var vpX:Number = Stage.width / 2; var vpY:Number = Stage.height / 2; var xOffset:Number = 0; var zOffset:Number = 400; var numFaces:Number = 20; init(); function init() {       var index:Number = 0;       for(var i:Number = 0;i<numFaces;i++)       {             var angle:Number = Math.PI * 2 / numFaces * i;             var x:Number = Math.cos(angle) * 200;             var y:Number = Math.sin(angle) * 200;             points[index] = {x:x, y:y, z:-100};             points[index + 1] = {x:x, y:y, z:100};             index += 2;       }       index = 0;       for(var i:Number = 0;i<numFaces-1;i++)       {             triangles[index] = {a:index,                                 b:index + 1,                                 c:index + 3,                                 col:0x6666cc};             triangles[index+1] = {a:index,                                   b:index + 3,                                   c:index + 2,                                   col:0x6666cc};             index += 2;       }       triangles[index] = {a:index,                           b:index + 1,                           c:1,                           col:0x6666cc};       triangles[index+1] = {a:index,                             b:1,                             c:0,                             col:0x6666cc}; } function onEnterFrame():Void {       if(Key.isDown(Key.LEFT))       {             xOffset -= 10;       }       else if(Key.isDown(Key.RIGHT))       {             xOffset += 10;       }       if(Key.isDown(Key.UP))       {             zOffset += 10;       }       else if(Key.isDown(Key.DOWN))       {             zOffset -= 10;       }       var angleY:Number = (_xmouse - vpX) * .001;       var cosY:Number = Math.cos(angleY);       var sinY:Number = Math.sin(angleY);       var angleX:Number = (_ymouse - vpY) * .001;       var cosX:Number = Math.cos(angleX);       var sinX:Number = Math.sin(angleX);       var numPoints:Number = points.length;       for (var i:Number=0;i<numPoints;i++) {             var point:MovieClip = points[i];             var x1:Number = point.x * cosY - point.z * sinY;             var z1:Number = point.z * cosY + point.x * sinY;             var y1:Number = point.y * cosX - z1 * sinX;             var z2:Number = z1 * cosX + point.y * sinX;             point.x = x1;             point.y = y1;             point.z = z2;             var scale:Number = fl / (fl + point.z + zOffset);             point.xPos = vpX + (point.x + xOffset) * scale;             point.yPos = vpY + point.y * scale;       }       clear();       var numTriangles:Number = triangles.length;       for(var i:Number =0;i<numTriangles;i++)       {             renderTriangle(triangles[i]);       } } function renderTriangle(tri:Object):Void {       beginFill(tri.col, 50);       lineStyle(1, 0, 10);       moveTo(points[tri.a].xPos, points[tri.a].yPos);       lineTo(points[tri.b].xPos, points[tri.b].yPos);       lineTo(points[tri.c].xPos, points[tri.c].yPos);       lineTo(points[tri.a].xPos, points[tri.a].yPos);       endFill(); } 


Foundation ActionScript. Animation. Making Things Move
Foundation Actionscript 3.0 Animation: Making Things Move!
ISBN: 1590597915
EAN: 2147483647
Year: 2005
Pages: 137
Authors: Keith Peters

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net