Home > Uncategorized > FollowMe

FollowMe

package
{
import away3d.cameras.lenses.OrthogonalLens;
import away3d.cameras.SpringCam;
import away3d.containers.View3D;
import away3d.primitives.Cube;
import away3d.primitives.Sphere;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Vector3D;
import flash.ui.Keyboard;
/**
* …
* @author Shiu
*/
public class FollowMe extends Sprite
{
private var _cube:Cube, _sphere1:Sphere, _sphere2:Sphere;
private var _view:View3D;
private var followerCam:SpringCam;
private var forward:Boolean, backward:Boolean, turnLeft:Boolean, turnRight:Boolean;

public function FollowMe()
{
followerCam = new SpringCam();

_view = new View3D(); addChild(_view);
_view.x = stage.stageWidth >> 1;
_view.y = stage.stageHeight >> 1;
_view.addEventListener(Event.ENTER_FRAME, update);
_view.camera = followerCam;

_cube = new Cube(); _view.scene.addChild(_cube);
_sphere1 = new Sphere(); _view.scene.addChild(_sphere1);
_sphere1.x += 300
_sphere2 = new Sphere(); _view.scene.addChild(_sphere2);
_sphere2.x -= 300

followerCam.target = _cube;
followerCam.positionOffset = new Vector3D(0, 200, -1000);
followerCam.stiffness = 5;
followerCam.damping = 20;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyFunction);
stage.addEventListener(KeyboardEvent.KEY_UP, keyFunction);
}

private function keyFunction(e:KeyboardEvent):void
{
if(e.type == “keyDown”){
if (e.keyCode == Keyboard.UP) forward = true;
else if (e.keyCode == Keyboard.DOWN) backward = true;
if (e.keyCode == Keyboard.LEFT) turnLeft = true;
else if (e.keyCode == Keyboard.RIGHT) turnRight = true;
}
else if (e.type == “keyUp”) {
if (e.keyCode == Keyboard.UP) forward = false;
else if (e.keyCode == Keyboard.DOWN) backward = false;
if (e.keyCode == Keyboard.LEFT) turnLeft = false;
else if (e.keyCode == Keyboard.RIGHT) turnRight = false;
}
}

private function update(e:Event):void {
if (forward) _cube.z += 100;
else if (backward) _cube.z -= 100;

if(turnLeft) _cube.rotationY += 10;
else if(turnRight) _cube.rotationY -= 10;

followerCam.view
_view.render();
}
}

}

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: