重點API:
sandy.util.LoaderQueue
sandy.core.scenegraph.Sprite2D
sandy.events.QueueEvent
- package
- {
- import flash.display.Bitmap;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.KeyboardEvent;
- import flash.events.MouseEvent;
- import flash.net.URLRequest;
- import sandy.core.Scene3D;
- import sandy.core.scenegraph.Camera3D;
- import sandy.core.scenegraph.Group;
- import sandy.core.scenegraph.Sprite2D;
- import sandy.events.QueueEvent;
- import sandy.util.LoaderQueue;
-
- public class Example004b extends Sprite
- {
- private var scene:Scene3D;
- private var camera:Camera3D;
- // Utility class for 載入外部資源
- private var queue:LoaderQueue;
- /** numTree 將影響 CPU 的效能 **/
- private var numTree:Number = 50;
- public function Example004b()
- {
- // 產生一 LoaderQueue 物件
- queue = new LoaderQueue();
- // 將欲載入資源之Unique名稱及路徑,加入 queue 中
- queue.add("tree", new URLRequest("asset/tree.gif"));
- // 為 queue 物件新增監聽事件 - 當資源載入完成時,
- // 執行 loadComplete Function
- queue.addEventListener(
- QueueEvent.QUEUE_COMPLETE,
- loadComplete);
- // 為 queue 物件新增監聽事件 - 當載入資源發生錯誤時,
- // 執行 loadError Function
- queue.addEventListener(
- QueueEvent.QUEUE_LOADER_ERROR,
- loadError);
- // 開始載入外部資源
- queue.start();
- }
- public function loadComplete(evt:QueueEvent):void
- {
- // 產生 Camera3D 物件
- camera = new Camera3D(600, 300);
- camera.y = 10;
- camera.z = -300;
-
- // 產生 Group 物件
- var root:Group = createScene();
- // 產生 Scene3D 物件,並將 Camera3D 及 Group 加入場景
- scene = new Scene3D("scene", this, camera, root);
- // 設定當物件超出場景之外時,將不會被畫出
- scene.rectClipping = true;
-
- addEventListener(Event.ENTER_FRAME,
- enterFrameHandler);
- stage.addEventListener(KeyboardEvent.KEY_DOWN,
- keyPressHandler);
- stage.addEventListener(MouseEvent.MOUSE_MOVE,
- mouseMoveHandler);
- }
- public function loadError(evt:QueueEvent):void
- {
- trace("load resource error");
- }
- private function createScene():Group
- {
- // 產生根 Group 物件
- var g:Group = new Group();
-
- // 產生 Sprite2D 物件
- for (var i:Number = 0; i <>
- {
- // 將載入 queue 物件中的資源取出
- var bit:Bitmap =
- new Bitmap(queue.data["tree"].bitmapData);
- // 產生 Sprite2D 物件,並設置其參數,分別為
- // Unique名稱,顯示物件 (DisplayObject) 及 縮放比率
- var s:Sprite2D = new Sprite2D("tree" + i, bit, 1);
- // 隨機設定 Sprite2D 的 x, y, z 座標位置
- s.x = Math.random() * 600 - 300;
- s.y = 0;
- s.z = Math.random() * 600;
-
- g.addChild(s);
- }
-
- return g;
- }
- private function enterFrameHandler(evt:Event):void
- {
- scene.render();
- }
- private function keyPressHandler(evt:KeyboardEvent):void
- {
- // 調整 camera 鏡頭之遠近
- switch(evt.keyCode)
- {
- case KeyboardEvent.KEY_UP:
- camera.moveForward(5);
- break;
- case KeyboardEvent.KEY_DOWN:
- camera.moveForward( -5);
- break;
- }
- }
- private function mouseMoveHandler(evt:MouseEvent):void
- {
- // 隨滑鼠移動設置 camera 之方向
- camera.pan = (evt.stageX - 300 / 2) / 10;
- camera.tilt = (evt.stageY - 300 / 2) / 20;
- }
- }
- }
程式碼出處:http://www.flashsandy.org/tutorials/3.0/sandy_cs3_tut042