sandy.core.scenegraph.TransformGroup
sandy.primitive.Cone
sandy.primitive.Hedra
- package
- {
- import flash.display.Sprite;
- import flash.events.*;
- import flash.ui.*;
- import sandy.core.Scene3D;
- import sandy.core.data.*;
- import sandy.core.scenegraph.*;
- import sandy.materials.*;
- import sandy.materials.attributes.*;
- import sandy.primitive.*;
- public class Example004 extends Sprite
- {
- private var scene:Scene3D;
- private var camera:Camera3D;
- private var tg:TransformGroup;
- private var myCone:Cone;
- private var myHeadra:Hedra;
- public function Example004()
- {
- // 產生 camera 物件
- camera = new Camera3D(300, 300);
- camera.x = 100;
- camera.y = 100;
- camera.z = -400;
- camera.lookAt(0, 0, 0);
- // 產生顯示物件群組
- var root:Group = createScene();
- // 產生場景物件,並將 camera 物件及顯示
- // 物件群組加到場景中
- scene = new Scene3D("scene", this, camera, root);
- addEventListener(Event.ENTER_FRAME, enterFrameHandler);
- stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
- }
- private function createScene():Group
- {
- var g:Group = new Group();
- // 在每個座標軸,產生一線性物件
- var myXLine:Line3D = new Line3D("x-coord", new Point3D(-50, 0, 0),
- new Point3D(50, 0, 0));
- var myYLine:Line3D = new Line3D("y-coord", new Point3D(0, -50, 0),
- new Point3D(0, 50, 0));
- var myZLine:Line3D = new Line3D("z-coord", new Point3D(0, 0, -50),
- new Point3D(0, 0, 50));
- // 產生一移動群組物件, 並為其命名
- tg = new TransformGroup("myGroup");
- // 產生Cone object, 並設定其參數,分別為:
- // 識別名稱, 半徑及高
- myCone = new Cone("theObj1", 50, 100);
- // 產生 Hedra objecct,並設定其參數,分別為:
- // 識別名稱、高、寬及深度
- myHeadra = new Hedra("theObj2", 80, 60, 60);
- // 分別將 Cone 物件移到左邊, Hedra 物件移到右邊
- myCone.x = -160;
- myCone.z = 150;
- myHeadra.x = 90;
- // 產生 materialAttributes物件
- var materialAttr:MaterialAttributes = new MaterialAttributes(
- new LineAttributes(0.5, 0x2111BB, 0.4),
- new LightAttributes(true, 0.1)
- );
- var material:Material = new ColorMaterial(0xFFCC33, 1, materialAttr);
- material.lightingEnable = true;
- var app:Appearance = new Appearance(material);
- myCone.appearance = app;
- myHeadra.appearance = app;
- // 將兩個 objects 加入移動群組物件中
- tg.addChild(myCone);
- tg.addChild(myHeadra);
- // 將所有的物件加入 Group 物件中
- g.addChild(tg);
- g.addChild(myXLine);
- g.addChild(myYLine);
- g.addChild(myZLine);
- return g;
- }
- private function enterFrameHandler(evt:Event):void
- {
- // 調整 Headra 與 Cone 沿Y軸轉向的幅度
- myHeadra.pan += 4;
- myCone.pan += 4;
- scene.render();
- }
- private function keyPressed(evt:KeyboardEvent):void
- {
- switch(evt.keyCode)
- {
- case Keyboard.UP:
- tg.y += 2;
- break;
- case Keyboard.DOWN:
- tg.y -= 2;
- break;
- case Keyboard.RIGHT:
- tg.roll += 2;
- break;
- case Keyboard.LEFT:
- tg.roll -= 2;
- break;
- }
- }
- }
- }
產出:
程式碼出處:http://www.flashsandy.org/tutorials/3.0/sandy_cs3_tut04
沒有留言:
張貼留言