2011年7月15日 星期五

文字欄位的捲動_TextField.scrollV

TextField.scrollV
垂直捲動的單位是字行,水平捲動的單位則是像素。 如果顯示的第一個字行是文字欄位中的第一個字行,scrollV 便會設定為 1 (而非 0)。
URLLoader.load()方法
public function load(request:URLRequest):void
從指定的 URL 傳送並載入資料。根據 dataFormat 屬性值,能以文字、原始二進位資料或 URL 編碼之變數的形式接收此資料。dataFormat 屬性的預設值為文字。
bottomScrollV屬性
bottomScrollV:int [唯讀]
整數 (從 1 開始的索引),指出目前在指定之文字欄位中,可以看見的最底端字行。 將文字欄位視為文字區塊上的視窗。 scrollV 屬性是視窗中,最頂端之可見字行的索引 (從 1 開始)。
在文字欄位中,scrollV 與 bottomScrollV 字行間的所有文字目前都是可見的。

※ 載入外部文字檔,由TextField讀取,倆顆按鈕控制向下和向上捲動文字行

1000714_TextField.scrollV.swf


package  {
 
 import flash.display.MovieClip;
 import flash.text.TextField;
 import flash.net.URLRequest;
 import flash.net.URLLoader;
 import flash.events.Event;
 import flash.display.SimpleButton;
 import flash.events.MouseEvent;
 import flash.display.DisplayObject;
 import flash.net.URLLoaderDataFormat;
 import flash.events.IOErrorEvent;
 import flash.errors.IOError;

 public class Main extends MovieClip {
  
  public var dynTF:TextField;
  public var str:String;
  public var nextBtn:SimpleButton;
  public var preBtn:SimpleButton;
  
  public function Main() {
   this.initURLLoader();
   
   /*滑鼠滾輪開關*/
   /*Boolean 值,指出當使用者按一下文字欄位並滾動滑鼠滾輪時,Flash Player 是否會自動捲動多行文字欄位。
   根據預設,這個值為 true。 如果想要防止滑鼠滾輪捲動文字欄位,或實作您自己的文字欄位捲動,這個屬性就會很有用。*/
   this.dynTF.mouseWheelEnabled = true;
   
   this.nextBtn.addEventListener(MouseEvent.MOUSE_DOWN,readNextLOOP);
   this.nextBtn.addEventListener(MouseEvent.MOUSE_UP,removeReadNextLOOP);
   this.preBtn.addEventListener(MouseEvent.MOUSE_DOWN,readPreLOOP);
   this.preBtn.addEventListener(MouseEvent.MOUSE_UP,removeReadPreLOOP);
   return;
  }
  
  private function initURLLoader():void{
   var urlReq:URLRequest = new URLRequest("http://dl.dropbox.com/s/y0eun5z88xxqw5e/1000714_TextField.scrollV_textStr2.txt");
   var urlLoader:URLLoader = new URLLoader();
   /**/
   urlLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
   /**/
   urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
   urlLoader.addEventListener(Event.COMPLETE,readData);
   /**/
   try{
    urlLoader.load(urlReq);
   }catch(error:Error){
    trace("Unable to load requested document.");
   }
   
   return;
  }
    
  private function readData(event:Event):void{
   var tmpLoader:URLLoader = event.currentTarget as URLLoader;
   /*this.str = tmpLoader.data.split("\r\n").join("\n");*/
   this.str = tmpLoader.data;
   trace(this.str);
   showText();
   return;
  }
  
  private function showText():void{
   this.dynTF.htmlText = this.str;
   trace("this.dynTF.maxScrollV : " + this.dynTF.maxScrollV);
   trace("this.dynTF.scrollV : " + this.dynTF.scrollV );
   trace("this.dynTF.bottomScrollV : " + this.dynTF.bottomScrollV );
   return;
  }
  
  private function readNextLOOP(event:MouseEvent):void{
   this.addEventListener(Event.ENTER_FRAME,this.readNext);
   return;
  }
  
  private function removeReadNextLOOP(event:MouseEvent):void{
   this.removeEventListener(Event.ENTER_FRAME,this.readNext);
   return;
  }
  
  private function readNext(event:Event):void{
   this.dynTF.scrollV += 1;
   trace("this.dynTF.scrollV : " + this.dynTF.scrollV );
   trace("this.dynTF.bottomScrollV : " + this.dynTF.bottomScrollV );
   return;
  }
  
  private function readPreLOOP(event:MouseEvent):void{
   this.addEventListener(Event.ENTER_FRAME,this.readPre);
   return;
  }
  
  private function removeReadPreLOOP(event:MouseEvent):void{
   this.removeEventListener(Event.ENTER_FRAME,this.readPre);
   return;
  }
  
  private function readPre(event:Event):void{
   this.dynTF.scrollV -= 1;
   trace("this.dynTF.scrollV : " + this.dynTF.scrollV);
   trace("this.dynTF.bottomScrollV : " + this.dynTF.bottomScrollV );
   return;
  }
  
  private function tween1(_targetObject:DisplayObject,_now:Number,_new:Number):void {
   return;
  }
  
  /**/
  private function ioErrorHandler(e:IOErrorEvent):void{
   trace("ioErroerHandler : " + e);
   return;
  }
 }
 
}

2011年6月23日 星期四

Flash執行階段錯誤

適用於 Adobe® Flash® Platform 的 ActionScript® 3.0 參考
下列是可能在執行階段發生的錯誤。
無論您在嚴謹模式或警告模式中進行編譯,ActionScript 3.0 都會進行執行階段類型檢查。

執行階段錯誤




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");
  }
 }
 
}