2012-04-17 38 views
0
使用回調

所以我們可以說我有以下代碼:如何在ActionScript

public function first(text:String):String { 
    _text = text; 
    dispatchEvent(event); 

    //Want this statement to return the value of _text 
    //after handler has finished transforming text. 
    return _text; 
} 

//handles the event 
public function handler(event:Event):void { 
    //does things, then changes the value of _text 
    _text = "next text that first needs to return"; 
} 

我將如何確保方法(第一)返回_text的正確的價值已經被改造後(處理) ?

預先感謝您!

+0

ActionScript使用的格式'名稱:Type'的參數,而不是'型name'。 – Marty 2012-04-17 00:39:35

+0

我曾經有過這樣的事情,但我在flex和java之間切換太多了,這就是混亂。感謝您指出:) – user220755 2012-04-17 00:41:26

+0

是的,我和C#之間有同樣的問題:P – Marty 2012-04-17 01:00:06

回答

0

由於ActionScript是單線程語言,事件處理程序不返回值我假設如果_text在程序包範圍內變量,您的代碼將工作。接下來的代碼不作大的意義,但如果你從另一個類調用first功能,它可以是有用

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 


    public class EventTest extends Sprite 
    { 
     public function EventTest() 
     { 
      addEventListener("sliceText", sliceHandler); 

      //will be Some 
      var newText:String = first("SomeText"); 
      trace(newText); 
     } 

     private var _text:String; 

     public function first(text:String):String 
     { 
      _text = text; 

      dispatchEvent(new Event("sliceText")); 

      return _text; 
     } 

     protected function sliceHandler(event:Event):void 
     { 
      //let's slice text to be more valuable 
      _text = _text.slice(0,4); 
     } 

    } 
} 
+0

一旦你調度事件, , 對? – user220755 2012-04-17 16:33:27

+0

不,正如我所說的,ActionScript是單線程語言,這意味着如果您調度一個事件,進程將轉到它的偵聽器,並且只有在所有偵聽器結束執行後,進程纔會在調度指令後進行。所以在我的例子中,sliceHandler將在return語句之前啓動。 – Art 2012-04-18 10:34:12