顯示具有 Loader 標籤的文章。 顯示所有文章
顯示具有 Loader 標籤的文章。 顯示所有文章

2011年6月22日 星期三

載入外部swf__父層子層雙向取用類別定義

LoaderContext()建構函式
public function LoaderContext(checkPolicyFile:Boolean = false, applicationDomain:ApplicationDomain = null, securityDomain:SecurityDomain = null)

getDefinitionByName()全域函數
public function getDefinitionByName(name:String):Object
會傳回 name 參數所指定之類別的類別物件參照。
參數
name:String — 類別的名稱。
傳回值
Object — 會傳回 name 參數所指定之類別的類別物件參照

※ 本例著眼於 main.swf 載入外部 child.swf,透過LoaderContext類別物件予以設定,則child.swf 中所有的ActionScript 3定義,同時將被載入至main.swf所在的目前應用程式網域,並能被main.swf所取用。事實上,child.swf 中的ActionScript 3定義被合併至main.swf所在的目前應用程式網域,導致main.swf與child.swf 彼此擁有對方類別定義的能見度。


※ main.swf 載入child.swf 之後,可調用child.swf 的Child 類別裡的 weicom()方法。
而,child.swf 則可調用 Main 類別裡的 hello()方法,與 Ball 類別裡的 ballFunc()方法。





※ 這個測試,將main.swf及child.swf隔開在兩個不同的套件根目錄中,以避免相同套件內的存取造成誤判。










Main.as
package  {
 
 import flash.display.Loader;
 import flash.display.Sprite;
 import flash.events.*;
 import flash.events.EventDispatcher;
 import flash.net.URLRequest;
 import flash.system.LoaderContext;
 import flash.system.ApplicationDomain;
 import flash.display.MovieClip;
 import flash.display.LoaderInfo;
 import flash.utils.getDefinitionByName;

 public class Main extends Sprite {
  
  public static const LOADED:String = "loaded";
  public var testVar:String;
  private var ldr:Loader;
  
  public function Main() {
   this.testVar = "abc";
   ldr = new Loader();
   var req:URLRequest = new URLRequest("../external/Child.swf");
   
   /*透過LoaderContext 指定 - 將外部Child.swf的公用類別定義載入到目前應用程式網域*/
   /* ***藉由將載入的 SWF 檔置於相同的應用程式網域中,便可以直接存取其類別。*** */
   var ldrContext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
   ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
   ldr.load(req,ldrContext);
  }

  private function completeHandler(event:Event=null):void {
   trace("@Main / completeHandler() / event.target.content : " + event.target.content);
   
   /*A法 : 使用ApplicationDomain.currentDomain.getDefinition()方法*/
   /*取得Child的類別參照,調用其建構式*/
   /*var getClass:Class = ApplicationDomain.currentDomain.getDefinition("Child") as Class;
   trace("@Main / completeHandler / getClass : " + getClass);*/
   
   /*B法 : 使用getDefinitionByName(name:String):Object*/ 
   /*public function getDefinitionByName(name:String):Object*/
   /*傳回 name 參數所指定之類別的類別物件參照*/
   /*參數   name:String — 類別的名稱。*/
   /*傳回值   Object — 會傳回 name 參數所指定之類別的類別物件參照。*/
   /*使用getDefinitionByName()函數,取得Child的類別參照,代表子層類別可見度ok*/
   var getClass:Class = getDefinitionByName("Child") as Class;
   trace("@Main / completeHandler / getClass : " + getClass);
   
   /*載入完成後, 建構一個Child類別實體, 指定給childObj - B法*/
   var childObj:* = new getClass();
   this.addChild(childObj);
   
   /*載入完成後發送自訂事件*/
   var loadedEvent:Event = new Event(LOADED);
   childObj.dispatchEvent(loadedEvent);
   trace("@Main / completeHandler() / this.dispatchEvent(loadedEvent) : " + this.dispatchEvent(loadedEvent));
   
   /*調用childObj實體的welcome()方法, */
   var message:String = childObj.welcome("Alice");
   trace("***********************************");
   trace("從Main調用Child的welcome()方法 - 以下這行");
   trace(message);
   trace("***********************************");
   return;
  }
  
  public function hello():String{
   var helloMessage:String = "@Main : Hello!!! Baby!!!";
   return helloMessage;
  }
 }
}



Child.as
package {

 import flash.display.Sprite;
 import flash.events.Event;
 import flash.system.ApplicationDomain;
 import flash.utils.getDefinitionByName;
 
 public class Child extends Sprite {
  
  public var getMainClass:Class;
  
  public function Child() { 
   if(ApplicationDomain.currentDomain.hasDefinition("Main")){
    /*A法 : 使用 ApplicationDomain.currentDomain.getDefinition()方法*/
    /*this.getMainClass = ApplicationDomain.currentDomain.getDefinition("Main") as Class;*/
    
    /*B法 : 使用getDefinitionByName(name:String):Object,取得Main的類別參照,代表父層類別可見度ok*/
    this.getMainClass = getDefinitionByName("Main") as Class;
    
    trace("@Child / Child() / getMainClass : " + this.getMainClass);
    this.monitor();
   }
   return;
  }
  
  public function monitor():void{
   /*偵聽Main.LOADED 之載入完成事件*/
   this.addEventListener(getMainClass.LOADED,loadedHandler);
   this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
   return;
  }
  
  public function loadedHandler(event:Event):void{
   this.removeEventListener(getMainClass.LOADED,loadedHandler);
   trace("@Child / loadedHandler() / event.type : " + event.type);
   
   /*取得父類別實體*/
   var myParent:* = getMainClass(this.parent);
   trace("@Child / loadedHandler() / myParent : " + myParent);
   
   /*調用Main的hello()方法*/
   trace("***********************************");
   trace("從Child調用Main的hello()方法 - 以下這行");
   var message:String = myParent.hello();
   trace(message);
   trace("***********************************");
   
   /*A法 : 使用 ApplicationDomain.currentDomain.getDefinition()方法*/
   /*取得Ball的類別參照,調用其建構式*/
   /*var getBallClass:Class = ApplicationDomain.currentDomain.getDefinition("Ball") as Class;*/
   
   /*B法 : 使用getDefinitionByName(name:String):Object,取得Ball的類別參照,代表父層類別可見度ok*/
   var getBallClass:Class = getDefinitionByName("Ball") as Class
   
   trace("@Child / loadedHandler() / getBallClass : " + getBallClass);
   var myParentBall:* = new getBallClass(160,200);
   trace("@Child / loadedHandler() / myParentBall : " + myParentBall);
   this.parent.addChild(myParentBall);
   return;
  }
  
  public function addedToStage(event:Event):void{
   this.removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
   this.addEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);
   return;
  }
  
  public function removedFromStage(event:Event):void{
   this.removeEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);
   return;
  }

  public function welcome(_name:String):String {
   return "@Child : Hello, " + _name;
  }
 }
}


Ball.as
package  {
 
 import flash.display.SimpleButton;
 import flash.events.Event;
 
 public class Ball extends SimpleButton {
  
  public var thisX:Number;
  public var thisY:Number;
  
  public function Ball(_x:Number,_y:Number) {
   this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
   this.thisX = _x;
   this.thisY = _y;
   return;
  }
  
  public function addedToStage(event:Event):void{
   this.removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
   this.addEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);
   this.x = this.thisX;
   this.y = this.thisY;
   return;
  }
  
  public function removedFromStage(event:Event):void{
   this.removeEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);
   return;
  }
  
  /*此例, Ball類別的這個ballFunc()方法,將在child.swf 被Main.swf 載入後,被Child類別實體調用*/
  public function ballFunc():void{
   trace("@Ball / ballFunc : I am from Ball Class");
  }
 }
 
}





值得注意這件事......

若把Main類別的Main建構函數的以下這行刪掉 :
var ldrContext: LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);

且把Main類別的Main建構函數的 ldr.load(req,ldrContext); 的 「,ldrContext」刪掉

[Ctrl+enter]測試影片, 將出現錯誤
ReferenceError: Error #1065: 變數 Child 未定義。
at global/flash.utils::getDefinitionByName()
at Main/completeHandler()

可見, 透過LoaderContext類別物件指定ApplicationDomain.currentDomain
將被載入child.swf 的公用類別定義置於目前載入器根swf所處的應用程式網域
就是本實例能夠正確運作的最大關鍵 ─ 父層子層雙向取用類別定義

若有人真的仔細看了code, 將會發現......
在main與Child裡, 由於處於父層子層彼此類別定義的透視度中
所以, 皆採用 [B法] 來取得對象的類別參照
即 getDefinitionByName(name:String):Object

然而, [A法] 的 ApplicationDomain.currentDomain.getDefinition()方法
則是,未取得父層子層彼此類別定義的透視度時的最佳利器.
透過 ApplicationDomain類別的[靜態]屬性currentDomain傳回目前應用程式網域物件(ApplicationDomain物件),
再調用getDefinition()方法取得類別參照.
很大的好處是,因為 ApplicationDomain.currentDomain為靜態屬性,
可在父層或子層直接呼叫, 達到子層取得父層各類別的目的
否則還真難達成.

前此, 那個 [大誤] 的例子, 最大的錯誤在於......
誤以為從相同網域載入的swf, 不需設定LoaderContext, 就立即與載入器根swf擁有相同應用程式網域
因此彼此擁有相互類別可見度. 其實不然啦.
再者, 沒有將main.as和child.as分開在不同套件目錄
則造成測試的混淆. 大誤阿 !!!

2011年6月21日 星期二

ApplicationDomain.currentDomain.getDefinition()

ApplicationDomain.currentDomain.getDefinition()
LoaderConText.applicationDomain屬性

◎◎◎ 從A.swf將B.swf載入
兩種方式調用getDefinition()方法
※ LoaderInfo.applicationDomain[唯讀].getDefinition()
※ ApplicationDomain.currentDomain[靜態] [唯讀].getDefinition()
※ LoaderInfo.applicationDomain[唯讀].getDefinition()

applicationDomain : ApplicationDomain
[唯讀] 當載入外部 SWF 檔時,所有包含在載入類別的 ActionScript 3.0 定義都會儲存在 applicationDomain 屬性中。
※ ApplicationDomain.currentDomain[靜態] [唯讀].getDefinition()
currentDomain : ApplicationDomain
[靜態] [唯讀] 會取得您的程式碼正在其中執行的目前應用程式網域。

getDefinition () 方法
public function getDefinition(name:String):Object
會從指定的應用程式網域取得公用定義。 定義可以是類別、名稱空間或函數的定義。
參數
name:String — 定義的名稱。
傳回值
Object — 與定義相關聯的物件。


不使用applicationDomain.getDefinition()前提下_載入外部swf_父層子層雙向取用類別

※※※ 1000622更新 : 目前發覺這個測試, 是在錯誤條件下得到的結果, 如果搬開child.swf到另一目錄, 就有錯誤發生. 暫時保留這個主題.


(一) 1000621_Main_1.fla 搭配主要類別(文件類別) Main_1_1000621.as,其元件庫中則有Ball元件綁定外部類別Ball.as,以上匯出為1000621_Main_1.swf。透過Main_1_1000621.as裡面的程式碼將載入外部的child.swf

(二) 被載入的外部swf 為child.swf, 由child.fla搭配Child.as所匯出.

(三) 1000621_Main_1.swf 載入child.swf後。

由於, child.swf 與 1000621_Main_1.swf  位於同一網域,且未以LoaderContext類別做其它設定
被載入的child.swf 與載入器所在的1000621_Main_1.swf,完成載入後,預設將位於同一安全性網域與同一應用程式網域
*** 藉由將載入的 SWF 檔置於相同的應用程式網域中,便可以直接存取其類別 *** 
*** 在載入器swf和被載入的swf之間,彼此所定義的類別為彼此可見 ***

(四) 所以,在Main_1_1000621文件類別中,可以直接調用Child類別建構式來建立其實體。
 var childObj:Child = new Child(); 
並調用其welcome()方法 - 
var message:String = childObj.welcome("Alice");

(五) 在Child.as中,可以直接調用Ball類別建構式,建立其實體。
var myParentBall:Ball = new Ball(275,200);
並調用其ballFunc()方法 - 
myParentBall.ballFunc();

(六) 以上, 質疑的是,是因為所解釋之原因,而能彼此擁有類別的可見度 ???
還是,因為命名空間的關係,而可被存取 ???


1000621_ApplicationDomain_1.rar 下載


Main_1_1000621
package  {
 
 import flash.display.Loader;
 import flash.display.Sprite;
 import flash.events.*;
 import flash.events.EventDispatcher;
 import flash.net.URLRequest;

 public class Main_1_1000621 extends Sprite {
  
  public static const LOADED:String = "loaded";
  public var testVar:String;
  private var ldr:Loader;
  
  public function Main_1_1000621() {
   this.testVar = "abc";
   ldr = new Loader();
   var req:URLRequest = new URLRequest("Child.swf");
   /*將外部Child.swf載入到目前應用程式網域*/
   /* ***藉由將載入的 SWF 檔置於相同的應用程式網域中,便可以直接存取其類別。*** */
   /*var ldrContext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);*/
   ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
   ldr.load(req);
  }

  private function completeHandler(event:Event=null):void {
   trace("@Main_1_1000621 / completeHandler() / event.target.content : " + event.target.content);
   /*取得ldr載入之Child類別實體 - A法*/
   /*var childObj:Child = event.target.content as Child; */
   
   /*載入完成後, 建構一個Child類別實體, 指定給childObj - B法*/
   var childObj:Child = new Child();
   this.addChild(childObj);
   
   /*調用childObj實體的welcome()方法, */
   var message:String = childObj.welcome("Alice");
   trace("@Main_1_1000621 / completeHandler() / message : " + message);// Hello, Alice 
   
   /*載入完成後發送自訂事件*/
   var loadedEvent:Event = new Event(LOADED);
   childObj.dispatchEvent(loadedEvent);
   trace("@Main_1_1000621 / completeHandler() / this.dispatchEvent(loadedEvent) : " + this.dispatchEvent(loadedEvent));
  }
  
  public function hello():void{
   trace("@Main_1_1000621 / hello() : Hello!!! Baby!!!");
  }
 }
}



Child
package {

 import flash.display.Sprite;
 import flash.events.Event;
 import flash.system.ApplicationDomain;
 
 public class Child extends Sprite {
  
  public var childVar:String;

  public function Child() {
   this.childVar = "@Child : I am from ChildClass";
   /*偵聽Main_1_1000621.LOADED 之載入完成事件*/
   this.addEventListener(Main_1_1000621.LOADED,loadedHandler);
   this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
   return;
  }
  
  public function loadedHandler(event:Event):void{
   trace("@Child / loadedHandler() / event.type : " + event.type);
   
   /*取得父類別實體*/
   var myParent:Main_1_1000621 = this.parent as Main_1_1000621;
   trace("@Child / loadedHandler() / myParent : " + myParent);
   
   /*調用父類實體的hello()方法*/
   myParent.hello();
   /*trace(myParent.testVar);*/
   return;
  }
  
  public function addedToStage(event:Event):void{
   this.removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
   this.addEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);
   
   /*Ball類別為1000621_Main_1.fla元件庫中Ball元件所綁定外部自訂類別*/
   
   /*由於, child.swf 與 1000621_Main_1.swf  位於同一網域, 且未以LoaderContext類別做其它設定*/
   /*被載入的child.swf 與載入器所在的1000621_Main_1.swf, 完成載入後, 預設將位於同一安全性網域與同一應用程式網域*/
   /* ***藉由將載入的 SWF 檔置於相同的應用程式網域中,便可以直接存取其類別。*** */
   
   /*因此,在這裡Child類別中,可以直接調用Ball類別建構式,建立其類別實體, 並調用Ball類別實體方法*/
   var myParentBall:Ball = new Ball(275,200);
   myParentBall.ballFunc();
   this.parent.addChild(myParentBall);
   return;
  }
  
  public function removedFromStage(event:Event):void{
   this.removeEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);
   return;
  }

  public function welcome(_name:String):String {
   return "Hello, " + _name;
  }
 }
}


Ball
package  {
 
 import flash.display.SimpleButton;
 import flash.events.Event;
 
 public class Ball extends SimpleButton {
  
  public var thisX:Number;
  public var thisY:Number;
  
  public function Ball(_x:Number,_y:Number) {
   this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
   this.thisX = _x;
   this.thisY = _y;
   return;
  }
  
  public function addedToStage(event:Event):void{
   this.removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
   this.addEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);
   this.x = this.thisX;
   this.y = this.thisY;
   return;
  }
  
  public function removedFromStage(event:Event):void{
   this.removeEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);
   return;
  }
  
  /*此例, Ball類別的這個ballFunc()方法,將在child.swf 被1000621_Main_1.swf 載入後,被Child類別實體調用*/
  public function ballFunc():void{
   trace("@Ball / ballFunc : I am from Ball Class");
  }
 }
 
}

2011年5月23日 星期一

DisplayObject.loaderInfo屬性參照LoaderInfo物件__取得目前swf的URL

DisplayObject.loaderInfo屬性傳回LoaderInfo物件
this.root.loaderInfo.url取得目前swf的URL,呈現在TextField
1000523_DisplayObject.loaderInfo.swf


package  {

import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

public class DisplayObject_loaderInfo_1000523 extends MovieClip {

public function DisplayObject_loaderInfo_1000523() {
/*flash.display.DisplayObject.loaderInfo屬性,傳回loaderInfo物件*/
/*var loaderPosStr:String = this.loaderInfo.url;  <---這樣也行*/
var loaderPosStr:String = this.root.loaderInfo.url;
this.createTF(decodeURIComponent(loaderPosStr));
return;
}

private function createTF(_loaderPosStr:String):void{
var tfFormat:TextFormat = new TextFormat();
tfFormat.size = 18;
tfFormat.color = 0x000088;

var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.wordWrap = true;
tf.multiline = true;
tf.defaultTextFormat = tfFormat;
tf.width = 450;
tf.x = 50;
tf.y = 50;
this.addChild(tf);
tf.text = _loaderPosStr;
return;
}
}

}

Loader.contentLoaderInfo屬性參照LoaderInfo物件__※取得目前swf或載入器swf的URL

Loader.contentLoaderInfo屬性
LoaderInfo.loaderURL屬性
透過 new Loader().contentLoaderInfo.loaderURL 抓取目前swf的URL位址,呈現在TextField
全域decodeURIComponent()函數
public function decodeURIComponent(uri:String):String
將已編碼的 URI 組件解碼成字串。傳回字串,當中所有先前由 encodeURIComponent 函數逸出的字元,都會還原成其未編碼的形式。HELP
※使用或不使用decodeURIComponent():在本機由 Flash CTRL+ENTER 或直接執行swf,可分辨其中不同。

1000523_Loader.contentLoaderInfo.loaderURL.swf


package  {

import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.display.Loader;
import flash.text.TextFormat;

public class LoaderURL_1000523 extends MovieClip {

public function LoaderURL_1000523() {
this.getLoaderURL();
return;
}
private function getLoaderURL():void{
var loaderPosStr:String = new Loader().contentLoaderInfo.loaderURL;
this.createTF(decodeURIComponent(loaderPosStr));
return;
}

private function createTF(_loaderPosStr:String):void{
var tfFormat:TextFormat = new TextFormat();
tfFormat.size = 18;
tfFormat.color = 0xffff00;

var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.wordWrap = true;
tf.multiline = true;
tf.defaultTextFormat = tfFormat;
tf.width = 450;
tf.x = 50;
tf.y = 50;
this.addChild(tf);
tf.text = _loaderPosStr;
return;
}
}

}

2011年1月1日 星期六

flash.utils套件__getDefinitionByName()函數 - 傳回所指定之類別的類別物件參照

getDefinitionByName()函數
public function getDefinitionByName(name:String):Object
傳回 name 參數所指定之類別的類別物件參照。
參數
name:String — 類別的名稱。
傳回值
Object — 會傳回 name 參數所指定之類別的類別物件參照。
擲回值
ReferenceError — 具有指定之名稱的公用定義不存在。




991231_GetDefinitionByName_1.swf



package  {
 import flash.utils.getDefinitionByName;
 import flash.display.Bitmap;
 import flash.display.BitmapData;
 import flash.display.Bitmap;
 import flash.display.Loader;
 //import flash.display.MovieClip;
 import flash.display.Sprite;
 import flash.net.URLRequest;
 import flash.events.Event;
 import flash.events.ProgressEvent;
 import flash.text.TextField;
 import flash.text.TextFieldAutoSize;
 //import flashx.textLayout.formats.FormatValue;
 import flash.text.TextFormat;

 public class GetDefinitionByName_1_991231 extends Sprite{
  //
  private var urlString:String = "991230_GetDefinitionByName_1.jpg";
  //
  //getDefinitionByName函數傳回flash.display.Loader類別的參照,指定給Class型別的classReference變數。
  private var classReference:Class = getDefinitionByName("flash.display.Loader") as Class;
  //new 運算子建構classReference類別實體,指定給Object型別的ldr變數。ldr已經參照了Loader類別。
  private var ldr:Object = new classReference();
  private var tf:TextField = new TextField();
  //
  public function GetDefinitionByName_1_991231() {
   setTextField();
   ldrFun();
  }
  
  //
  function setTextField():void{
   //
   var format:TextFormat = new TextFormat();
   with(format){
    color = 0xffffff;
    size = 86;
    font = "Arial";
   }
   //
   addChild(tf);
   with(tf){
    defaultTextFormat = format;
    x = 0;
    y = stage.stageHeight/2 -50 ;
    width = stage.stageWidth;
    autoSize = TextFieldAutoSize.CENTER;
   }
  };
  
  //
  private function ldrFun():void{
   ldr.load(new URLRequest(urlString));
   trace(urlString);
   ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoaderProgress);
   ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
  }
  
  //
  private function onLoaderProgress(e:ProgressEvent):void{
   tf.text = String(Math.floor(e.bytesLoaded/e.bytesTotal*100));
  }
  
  //
  private function onComplete(e:Event):void{
   trace(ldr); // [object LoaderInfo]
   var ldrBm:Bitmap = e.currentTarget.content as Bitmap;
   var bmd :BitmapData = ldrBm.bitmapData;
   var bm:Bitmap = new Bitmap(bmd);
   trace(bmd); // [object BitmapData]
   addChild(bm);
   bm.x = 50;
   bm.y = 30;
  }

 }
 
}


適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__●flash.utils_getDefinitionByName()函數

2010年12月10日 星期五

[AS3] 將Loader類別所載入外部點陣圖BitmapData資料轉植至新建的Bitmap類別實體

Bitmap()建構函式
public function Bitmap(bitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false)
new Bitmap((Loader.content as Bitmap).bitmapData);

991210_BitmapData_1.swf



import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Bitmap;

//
const bmpURL:String = "991210_1.jpg";
//
var ldr:Loader = new Loader();
ldr.load(new URLRequest(bmpURL));
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, bmpWork);
//
var showBmp1:Bitmap;
var showBmp2:Bitmap;
//
function bmpWork(e:Event):void{
 //
 var ldrBmp:Bitmap = ldr.content as Bitmap;
 //
 showBmp1 = new Bitmap(ldrBmp.bitmapData);
 showBmp1.x = 0;
 showBmp1.y = 0;
 addChild(showBmp1);
 //
 showBmp2 = new Bitmap(ldrBmp.bitmapData);
 showBmp2.scaleX = 0.3;
 showBmp2.scaleY = 0.3;
 showBmp2.x = 370;
 showBmp2.y = 255;
 addChild(showBmp2);
}


加上預載進度百分比



import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Bitmap;
import flash.events.ProgressEvent;
import flash.text.TextField;
import flash.text.TextFormat;

//
var format:TextFormat = new TextFormat();
format.size = 88;
format.color = 0xffff00;
//
var tF:TextField = new TextField();
tF.x = 220;
tF.y = 150;
tF.autoSize = TextFieldAutoSize.LEFT;
tF.defaultTextFormat = format;
addChild(tF);

//
const bmpURL:String = "991210_1.jpg";
//
var ldr:Loader = new Loader();
ldr.load(new URLRequest(bmpURL));
//
ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoaderProgress);
//
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, bmpWork);

//
function onLoaderProgress(e:ProgressEvent):void{
 tF.text = String(Math.floor(e.bytesLoaded/e.bytesTotal*100));
}

//
var showBmp1:Bitmap;
var showBmp2:Bitmap;
//
function bmpWork(e:Event):void{
 //
 var ldrBmp:Bitmap = ldr.content as Bitmap;
 //
 showBmp1 = new Bitmap(ldrBmp.bitmapData);
 showBmp1.x = 0;
 showBmp1.y = 0;
 addChild(showBmp1);
 //
 showBmp2 = new Bitmap(ldrBmp.bitmapData);
 showBmp2.scaleX = 0.3;
 showBmp2.scaleY = 0.3;
 showBmp2.x = 370;
 showBmp2.y = 255;
 addChild(showBmp2);
}


適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__●flash.display.Loader.content屬性
適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__●flash.display.Bitmap類別
適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__●flash.events.ProgressEvent.PROGRESS

2010年11月30日 星期二

[AS3] 亂數隨機載入外部圖檔(輪播)_每一輪不重覆

外部有6個圖檔,依亂數載入。同一輪已載入過的就不再載入。第1輪播完再播第2輪......



//991228從991130_randomPhoto_1.fla修改

import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
//載入Tweener、ColorShortcuts類別
//請至以下URL下載tweener類別程式庫與說明文件
//http://code.google.com/p/tweener/
import caurina.transitions.Tweener;
import caurina.transitions.properties.ColorShortcuts;
//初始化ColorShortcuts
ColorShortcuts.init();

//****** photo陣列 - push() 加入外部圖檔檔名
var photo:Array = new Array();
photo.push("991130_p1.jpg");
photo.push("991130_p2.jpg");
photo.push("991130_p3.jpg");
photo.push("991130_p4.jpg");
photo.push("991130_p5.jpg");
photo.push("991130_p6.jpg");

//
var pNum:uint = photo.length;

//****** intervalDuration - 換圖秒數(毫秒)
//目前設為 5000豪秒,即 5 秒
var intervalDuration:Number = 5000;

//TextFormat物件設定文字格式
var aTextFormat:TextFormat = new TextFormat();
aTextFormat.size = 18;
aTextFormat.color = 0xffffff;
aTextFormat.leftMargin = 5;
aTextFormat.rightMargin = 5;
//宣告TextField文字欄位類別實體infoTextField
var infoTextField:TextField = new TextField();
infoTextField.x = 15;
infoTextField.y = 15;
infoTextField.background = true;
infoTextField.backgroundColor = 0xff2200;
infoTextField.autoSize = TextFieldAutoSize.LEFT;
//將 infoTextField 加入顯示清單
addChild(infoTextField);
//建構Loader類別實體ldr
var ldr:Loader = new Loader();
//
ini();
//
function ini():void
{
 loadPhoto();
}
//
function loadPhoto()
{
 //如果photo陣列元素都被移除掉了
 //以for迴圈重新加入各元素,即外部檔名,同初始值
 if(photo.length == 0){
  for (var i:uint=0; i < pNum; i++) { // pNum
   photo[i] = "991130_p" + String(i+1) + ".jpg";
  }
  trace("------------------------------------------");
 }
 
 //Math.random() 亂數產生0(含)~1(不含)之浮點數, 
 //乘以圖檔張數(即photo陣列長度 photo.length),會得到 0(含)~6(不含) 之符點數
 //Math.floor - 去掉小數 - 會得到0~5之正整數
 var photoRandom:uint = Math.floor(Math.random() * photo.length);
 //photo[photoRandom] - 因應亂數,做為陣列索引,指向陣列元素
 var photoURL:String = photo[photoRandom];
 //splice(起始索引, 刪除數量) - 刪除被選定的陣列元素
 //如此在每一循環載入圖檔,才不會重覆
 photo.splice(photoRandom, 1);
 trace(photoURL);
 //將photoURL所存的圖檔檔名字串指定給 infoTextField 文字欄位顯示
 infoTextField.text = photoURL;
 //setTextFormat()方法,將aTextFormat格式化物件與infoTextField文字欄位關聯
 infoTextField.setTextFormat(aTextFormat);
 //
 //Loader類別實體ldr,以load()方法載入new URLRequest()的相對網址(即圖檔檔名)
 ldr.load(new URLRequest(photoURL));
 //將Event.COMPLETE事件的偵聽程式onLoaderComplete,附加到Loader類別實體ldr的contentLoaderInfo屬性 
 ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
}

//
function onLoaderComplete(e:Event):void
{
 addChild(ldr);
 //將ldr可視物件實體,以x,y座標控制在舞台中央
 ldr.x =  (stage.stageWidth - ldr.width) / 2;
 ldr.y =  (stage.stageHeight - ldr.height) / 2;
 //呼叫漸變屬性設定函式tweenPhoto1()
 tweenPhoto1();
 //setTimeout(closure:Function, delay:Number, ... arguments):uint
 //在指定的延遲時間 (以毫秒為單位) 之後執行指定的函數
 var intervalId:uint = setTimeout(ini, intervalDuration);
}
//
function tweenPhoto1():void{
 Tweener.addTween(ldr, {alpha:0, scaleX:0.9, scaleY:0.9, time:0, onComplete:tweenPhoto2});
} 
function tweenPhoto2():void{
 Tweener.addTween(ldr, {alpha:1, scaleX:1, scaleY:1, time:1.5});
} 

◎另需參閱
指定類別檔路徑 - 以下載及設置Tweener類別庫為例 http://flash.twgogo.org/2010/12/blog-post.html