/*在fb下新建as项目运行此实例*/
Basic07_apply.as
package
{
import away3d.containers.View3D;
import away3d.core.math.Number3D;
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_apply extends Sprite
{
private var view:View3D;
private var cube:Cube;
private var toggle:uint = 0;
private var cubeSpeed:Number = 5;
private var label:TextField;
public function Basic07_apply()
{
// add a button to re-set the rotation (rotate mesh, but not object valuse)
var bt1:Button = new Button("Apply the current rotation as new Zero",260,20);
bt1.x = 10;
bt1.y = 60;
bt1.addEventListener(MouseEvent.CLICK,applyRotations);
this.addChild(bt1);
// add a button to toggle between the two ways of moving
var bt2:Button = new Button("Toggle pivot point",260,20);
bt2.x = 10;
bt2.y = 90;
bt2.addEventListener(MouseEvent.CLICK,applyPivot);
this.addChild(bt2);
// Add a textfield
label = new 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);
// create a viewport
view = new View3D({x:250,y:200,renderer:Renderer.INTERSECTING_OBJECTS});
addChild(view);
view.camera.y = 300; // Move camera up
view.camera.lookAt( new Number3D(0,0,0)); // Point it toward scene center again, so the target is right (setting y does not change where the camera is pointing)
// create a rectangular cube
cube = new Cube({material:"green#black",depth:200,width:10,height:10});
// add cube to scene
view.scene.addChild(cube);
// create a sphere for reference
var sphere:Sphere = new Sphere({material:"red#black",radius:25});
view.scene.addChild(sphere);
// render the view
this.addEventListener(Event.ENTER_FRAME,update);
}
private function applyRotations(e:MouseEvent):void
{
cube.rotationY = 45;
cube.applyRotations();
toggle = 1;
}
// simply applies a new position to the mesh
private function applyPivot(e:MouseEvent):void
{
if(toggle == 2){ // reset pivot
toggle = 0;
cube.movePivot(0,0,0);
} else {
toggle = 2;
cube.movePivot(0,0,-120);
}
}
private function update(e:Event):void
{
// Keep cube within bounds
cube.rotationY += 1;
if(toggle == 1){
label.text = "Using cube.applyRotations. RotationY is now: "+Math.floor(cube.rotationY);
} else if (toggle == 2){
label.text = "The altered pivot point of 0,0,-120 is now used. Cube.z is now: "+Math.floor(cube.x);
} else {
label.text = "The standard pivot point of 0,0,0 is now used. Cube.z is now: "+Math.floor(cube.x);
}
// re-render the view
view.render();
}
}
}