/** * ... * @author ever5u */ public class fuxi extends Sprite { private var scene:Scene3D; private var camera:Camera3D; private var queue:LoaderQueue; private var numTree:Number = 50;
public function fuxi() { queue = new LoaderQueue(); queue.add( "tree", new URLRequest("tree.gif") ); queue.addEventListener(SandyEvent.QUEUE_COMPLETE, loadComplete ); queue.start(); } public function loadComplete(event:QueueEvent ):void { //先創(chuàng)建攝像機(jī) camera = new Camera3D( 600, 300 ); camera.y = 10; camera.z = -300; //創(chuàng)建 Group var root:Group = createScene(); //創(chuàng)建場(chǎng)景 scene = new Scene3D( "scene", this, camera, root ); scene.rectClipping = true; addEventListener( Event.ENTER_FRAME, enterFrameHandler ); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedHandler); stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMovedHandler); } private function createScene():Group { var g:Group = new Group(); for(var i:Number=0; i<numTree; i++){ var bit:Bitmap = new Bitmap(queue.data["tree"].bitmapData); var s:Sprite2D = new Sprite2D("tree"+i,bit,1); s.x = Math.random()*600 - 300; s.z = Math.random()*600; s.y = 0; g.addChild(s); } return g; } private function enterFrameHandler( event : Event ) : void { scene.render(); } private function keyPressedHandler(event:KeyboardEvent):void { switch(event.keyCode) { case Keyboard.UP: camera.moveForward(5); break; case Keyboard.DOWN: camera.moveForward(-5); break; } } private function mouseMovedHandler(event:MouseEvent):void { camera.pan=(event.stageX-300/2)/10; camera.tilt=(event.stageY-300/2)/20; } } }