topical media & game development
student-papervision-samples-CF03.ax
student-papervision-samples-CF03.ax
[swf]
flex
package {
import student_papervision_samples_PaperBase;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.materials.BitmapFileMaterial;
import caurina.transitions.Tweener;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.text.TextFormat;
public class @ax-student-papervision-samples-CF03 extends student_papervision_samples_PaperBase {
public var updown:Boolean = false;
public var leftdown:Boolean = false;
public var downdown:Boolean = false;
public var rightdown:Boolean = false;
public var speed:Number = 150; // Here you can set the speed of the object.
public var text:TextField = new TextField();
public var rotationPosition:Number = 0; // New Variable
public var rotationGoal:Number = 0; // New Variable
public var rotationBase:Number = 0; // New Variable
private var fontstyle:TextFormat = new TextFormat("Arial",44,0x000000);
private var cube:Cube;
private var cube2:Cube;
private var mat:FlatShadeMaterial = new FlatShadeMaterial(new PointLight3D(), 0xFFFFFF, 0xFF0000);
private var mat2:WireframeMaterial = new WireframeMaterial(0x00FF00);
private var plane:Plane = new Plane(null, 2000, 2000, 10, 10);
public function @ax-student-papervision-samples-CF03() {
init(600, 300);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
override protected function init3d():void{
cube = new Cube(new MaterialsList( { all: mat } ), 100, 100, 100);
cube.y = 0;
cube2 = new Cube(new MaterialsList( { all: mat2 } ), 100, 100, 100);
cube2.y = 0;
cube2.x = 1000;
cube2.z = 1000;
plane.material.lineColor = 0x777777;
plane.material.doubleSided = true;
plane.pitch(90);
plane.y = -50;
default_scene.addChild(plane);
default_scene.addChild(cube);
default_scene.addChild(cube2);
Tweener.addTween(cube, { x:1000, z:1000, time:2, onComplete: randomize } );
default_camera.x = 0;
default_camera.z = 1000;
default_camera.y = 1000;
}
override protected function init2d():void {
addChild(text);
text.selectable = false;
text.setTextFormat(fontstyle);
text.width = 800;
text.text = "Multimedia Authoring - ~vr0815";
}
public function randomize():void {
cube2.x = cube.x;
cube2.z = cube.z;
}
public function onKeyDown( event:KeyboardEvent ):void {
switch(event.keyCode) {
case 38:
updown = true;
break;
case 37:
leftdown = true;
break;
case 40:
downdown = true;
break;
case 39:
rightdown = true;
break;
}
}
public function onKeyUp( event:KeyboardEvent ):void {
switch(event.keyCode) {
case 38:
updown = false;
break;
case 37:
leftdown = false;
break;
case 40:
downdown = false;
break;
case 39:
rightdown = false;
break;
}
}
override protected function processFrame():void {
var xp:Number = 0;
var yp:Number = 0;
if(rotationBase == rotationPosition % 360) {
if (updown && leftdown) {
xp = cube.x + speed;
yp = cube.z + speed;
rotationGoal = 315; // RotationGoal keeps track of which direction you want to rotate to, in degrees.
Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
} else if (updown && rightdown) {
xp = cube.x + speed;
yp = cube.z - speed;
rotationGoal = 45;
Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
} else if (downdown && leftdown) {
xp = cube.x - speed;
yp = cube.z + speed;
rotationGoal = 225;
Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
} else if (downdown && rightdown) {
xp = cube.x - speed;
yp = cube.z - speed;
rotationGoal = 135;
Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
} else if (updown) {
xp = cube.x + speed;
yp = cube.z;
rotationGoal = 0;
Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
} else if (downdown) {
xp = cube.x - speed;
yp = cube.z;
rotationGoal = 180;
Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
} else if (leftdown) {
xp = cube.x;
yp = cube.z + speed;
rotationGoal = 270;
Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
} else if (rightdown) {
xp = cube.x;
yp = cube.z - speed;
rotationGoal = 90;
Tweener.addTween(cube, { x:xp, z:yp, time:2, onComplete: randomize } );
}
}
if (rotationPosition % 360 != rotationGoal) { //this piece of code rotates the player left or right, depending on which route is shorter.
if((((rotationBase % 360 - rotationGoal) < 0) && ((rotationBase % 360 - rotationGoal) > -181)) || ((rotationBase % 360 - rotationGoal) > 180)) {
cube.rotationY = cube.rotationY + 15;
rotationPosition = rotationPosition + 15;
} else {
cube.rotationY = cube.rotationY - 15;
rotationPosition = rotationPosition - 15;
}
} else {
rotationBase = rotationPosition % 360;
}
Tweener.addTween(default_camera, { x:cube.x - 800, z:cube.z, time:2, onComplete: randomize } )
default_camera.lookAt(cube);
rotationPosition = rotationPosition % 360;
// The next code is about a rather nasty bug from actionscript's modulo-function (%).
// It took me ages to figure out that the %-function doesn't check whether a number is positive or negative. This code fixes that part for rotationPosition
if (rotationPosition < 0) {
rotationPosition = rotationPosition + 360;
}
}
}
}
(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.