away3D操纵三维物体之方块横穿地球实例

/*在fb下新建as项目运行此实例*/
Basic07_moveForward.as

package{
	import away3d.containers.View3D;
	import away3d.core.render.Renderer;
	import away3d.primitives.Cube;
	import away3d.primitives.Sphere;
	import away3d.test.Button;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.*;
	
	[SWF(width="500", height="400", frameRate="60", backgroundColor="#FFFFFF")]
	
	public class Basic07_moveForward extends Sprite{
		private var view:View3D;
		private var cube:Cube;
		private var toggle:Boolean = false;
		private var bt:Button;
		private var cubeSpeed:Number = 5;
		private var label:TextField;
		
		public function Basic07_moveForward(){
			bt = new Button("toggle", 80);    // add a button to toggle between the two ways of moving
			bt.x = 10;
			bt.y = 60;
			bt.addEventListener(MouseEvent.CLICK, resetCube);
			this.addChild(bt);
			label = new TextField();    // Add a textfield
            label.autoSize = TextFieldAutoSize.LEFT;
            label.multiline = true;
            label.wordWrap = true;
            label.x = 10;
            label.y = 10;
            label.width = 480;
            label.defaultTextFormat = new TextFormat("Arial", 14, 0x000000);
            this.addChild(label);
			view = new View3D({x:250, y:200, radius:75, renderer:Renderer.INTERSECTING_OBJECTS});
			addChild(view);
			// create a sphere and put it on the 3D stage
			var sphere:Sphere = new Sphere({material:"red#yellow", radius:75});
			view.scene.addChild(sphere);
			// create a rectangular cube
			cube = new Cube({material:"green#black", depth:200, width:50, height:50});
			cube.rotationY = 45;    // We must rotate the cube, or you won't see any difference
			resetCube();    // set the cube position
			view.scene.addChild(cube);    // add cube to scene
			this.addEventListener(Event.ENTER_FRAME, update);
		}
		
		private function resetCube(e:MouseEvent = null):void{
			cube.x = 0;
			cube.y = 0;
			cube.z = 0;
			if(toggle){    // toggle movement method
				toggle = false;
				label.text = "Using cube.moveForward:\nThe cube moves forward according to it's rotation.";
			}else{
				toggle = true;
				label.text = "Using cube.z:\nThe cube moves relative to it's parent object (the view) ";
			}
		}
		
		private function update(e:Event):void{
			// Keep cube within bounds
			if(cube.z+cubeSpeed > 200 || cube.z + cubeSpeed < -200){
				cubeSpeed = cubeSpeed * (-1);
			}
			// use move method depending on toggle value
			if(toggle){
				cube.z += cubeSpeed;
			}else{
				cube.moveForward(cubeSpeed);
			}
			view.render();
		}
		
	}
}

你可能感兴趣的:(Flash)