2009年7月14日 星期二

Tutorial 04 - Start Moving

重點API:
sandy.core.scenegraph.TransformGroup
sandy.primitive.Cone
sandy.primitive.Hedra


  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.*;
  5. import flash.ui.*;
  6. import sandy.core.Scene3D;
  7. import sandy.core.data.*;
  8. import sandy.core.scenegraph.*;
  9. import sandy.materials.*;
  10. import sandy.materials.attributes.*;
  11. import sandy.primitive.*;
  12. public class Example004 extends Sprite
  13. {
  14. private var scene:Scene3D;
  15. private var camera:Camera3D;
  16. private var tg:TransformGroup;
  17. private var myCone:Cone;
  18. private var myHeadra:Hedra;
  19. public function Example004()
  20. {
  21. // 產生 camera 物件
  22. camera = new Camera3D(300, 300);
  23. camera.x = 100;
  24. camera.y = 100;
  25. camera.z = -400;
  26. camera.lookAt(0, 0, 0);
  27. // 產生顯示物件群組
  28. var root:Group = createScene();
  29. // 產生場景物件,並將 camera 物件及顯示
  30. // 物件群組加到場景中
  31. scene = new Scene3D("scene", this, camera, root);
  32. addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  33. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
  34. }
  35. private function createScene():Group
  36. {
  37. var g:Group = new Group();
  38. // 在每個座標軸,產生一線性物件
  39. var myXLine:Line3D = new Line3D("x-coord", new Point3D(-50, 0, 0),
  40. new Point3D(50, 0, 0));
  41. var myYLine:Line3D = new Line3D("y-coord", new Point3D(0, -50, 0),
  42. new Point3D(0, 50, 0));
  43. var myZLine:Line3D = new Line3D("z-coord", new Point3D(0, 0, -50),
  44. new Point3D(0, 0, 50));
  45. // 產生一移動群組物件, 並為其命名
  46. tg = new TransformGroup("myGroup");
  47. // 產生Cone object, 並設定其參數,分別為:
  48. // 識別名稱, 半徑及高
  49. myCone = new Cone("theObj1", 50, 100);
  50. // 產生 Hedra objecct,並設定其參數,分別為:
  51. // 識別名稱、高、寬及深度
  52. myHeadra = new Hedra("theObj2", 80, 60, 60);
  53. // 分別將 Cone 物件移到左邊, Hedra 物件移到右邊
  54. myCone.x = -160;
  55. myCone.z = 150;
  56. myHeadra.x = 90;
  57. // 產生 materialAttributes物件
  58. var materialAttr:MaterialAttributes = new MaterialAttributes(
  59. new LineAttributes(0.5, 0x2111BB, 0.4),
  60. new LightAttributes(true, 0.1)
  61. );
  62. var material:Material = new ColorMaterial(0xFFCC33, 1, materialAttr);
  63. material.lightingEnable = true;
  64. var app:Appearance = new Appearance(material);
  65. myCone.appearance = app;
  66. myHeadra.appearance = app;
  67. // 將兩個 objects 加入移動群組物件中
  68. tg.addChild(myCone);
  69. tg.addChild(myHeadra);
  70. // 將所有的物件加入 Group 物件中
  71. g.addChild(tg);
  72. g.addChild(myXLine);
  73. g.addChild(myYLine);
  74. g.addChild(myZLine);
  75. return g;
  76. }
  77. private function enterFrameHandler(evt:Event):void
  78. {
  79. // 調整 Headra 與 Cone 沿Y軸轉向的幅度
  80. myHeadra.pan += 4;
  81. myCone.pan += 4;
  82. scene.render();
  83. }
  84. private function keyPressed(evt:KeyboardEvent):void
  85. {
  86. switch(evt.keyCode)
  87. {
  88. case Keyboard.UP:
  89. tg.y += 2;
  90. break;
  91. case Keyboard.DOWN:
  92. tg.y -= 2;
  93. break;
  94. case Keyboard.RIGHT:
  95. tg.roll += 2;
  96. break;
  97. case Keyboard.LEFT:
  98. tg.roll -= 2;
  99. break;
  100. }
  101. }
  102. }
  103. }

產出:



程式碼出處:http://www.flashsandy.org/tutorials/3.0/sandy_cs3_tut04

沒有留言:

張貼留言