topical media & game development
animation-ch16-MovingCube.ax
animation-ch16-MovingCube.ax
[swf]
[flash]
flex
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class @ax-animation-ch16-MovingCube extends Sprite
{
private var points:Array;
private var triangles:Array;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var offsetX:Number = 0;
private var offsetY:Number = 0;
public function @ax-animation-ch16-MovingCube()
{
init();
}
private function init():void
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
points = new Array();
// front four corners
points[0] = new animation_ch16_Point3D(-100, -100, -100);
points[1] = new animation_ch16_Point3D( 100, -100, -100);
points[2] = new animation_ch16_Point3D( 100, 100, -100);
points[3] = new animation_ch16_Point3D(-100, 100, -100);
// back four corners
points[4] = new animation_ch16_Point3D(-100, -100, 100);
points[5] = new animation_ch16_Point3D( 100, -100, 100);
points[6] = new animation_ch16_Point3D( 100, 100, 100);
points[7] = new animation_ch16_Point3D(-100, 100, 100);
for(var i:uint = 0; i < points.length; i++)
{
points[i].setVanishingPoint(vpX, vpY);
points[i].setCenter(0, 0, 200);
}
triangles = new Array();
// front
triangles[0] = new animation_ch16_Triangle(points[0], points[1], points[2], 0x6666cc);
triangles[1] = new animation_ch16_Triangle(points[0], points[2], points[3], 0x6666cc);
// top
triangles[2] = new animation_ch16_Triangle(points[0], points[5], points[1], 0x66cc66);
triangles[3] = new animation_ch16_Triangle(points[0], points[4], points[5], 0x66cc66);
//back
triangles[4] = new animation_ch16_Triangle(points[4], points[6], points[5], 0xcc6666);
triangles[5] = new animation_ch16_Triangle(points[4], points[7], points[6], 0xcc6666);
// bottom
triangles[6] = new animation_ch16_Triangle(points[3], points[2], points[6], 0xcc66cc);
triangles[7] = new animation_ch16_Triangle(points[3], points[6], points[7], 0xcc66cc);
// right
triangles[8] = new animation_ch16_Triangle(points[1], points[5], points[6], 0x66cccc);
triangles[9] = new animation_ch16_Triangle(points[1], points[6], points[2], 0x66cccc);
// left
triangles[10] = new animation_ch16_Triangle(points[4], points[0], points[3], 0xcccc66);
triangles[11] = new animation_ch16_Triangle(points[4], points[3], points[7], 0xcccc66);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function onEnterFrame(event:Event):void
{
var angleX:Number = (mouseY - vpY) * .001;
var angleY:Number = (mouseX - vpX) * .001;
for(var i:uint = 0; i < points.length; i++)
{
var point:animation_ch16_Point3D = points[i];
point.rotateX(angleX);
point.rotateY(angleY);
}
graphics.clear();
for(i = 0; i < triangles.length; i++)
{
triangles[i].draw(graphics);
}
}
private function onKeyDown(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.LEFT)
{
offsetX -= 5;
}
else if(event.keyCode == Keyboard.RIGHT)
{
offsetX += 5;
}
else if(event.keyCode == Keyboard.UP)
{
offsetY -= 5;
}
else if(event.keyCode == Keyboard.DOWN)
{
offsetY += 5;
}
for(var i:Number = 0; i < points.length; i++)
{
points[i].setCenter(offsetX, offsetY, 200);
}
}
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.