2011-02-15 36 views
0

我有這個類( 'Scheduler.as'):ActionScript 3自定義「稍後調用」幾乎可以工作,專家希望!

package 
{ 
    import flash.events.TimerEvent; 
    import flash.utils.Timer; 

    public class Scheduler 
    { 
     private var m_tmr:Timer = null; 

     private var m_the_this:* = null; 
     private var m_function:Function = null; 
     private var m_args:Array = null; 

     public function Scheduler(the_this:*, f:Function, interval:int, args:Array = null) 
     { 
      this.m_the_this = the_this; 
      this.m_function = f; 
      this.m_args = args; 

      if (this.m_args.length == 0) 
       this.m_args = null; 

      this.m_tmr = new Timer(interval, 1); 
      this.m_tmr.addEventListener(TimerEvent.TIMER, on_timer); 
      this.m_tmr.start(); 
     } 

     private function on_timer(e:TimerEvent):void 
     { 
      if (this.m_args == null) 
       this.m_function.call(this.m_the_this); 
      else 
       this.m_function.call(this.m_the_this, this.m_args); 
     } 

     public static function schedule_call(the_this:*, f:Function, interval:int, ...args):Scheduler 
     { 
      return new Scheduler(the_this, f, interval, args); 
     } 
    } 
} 

而且這裏有一個使用它的AS3的FlashDevelop應用程序( 'Main.as'):

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

    public class Main extends Sprite 
    { 
     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 
     } 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 
      // entry point 

      Scheduler.schedule_call(this, this.test_func_NO_PARAMS, 0); 
      Scheduler.schedule_call(this, this.test_func_ONE_PARAM, 0, 123); 
      Scheduler.schedule_call(this, this.test_func_TWO_PARAMS, 0, "HELLO", "WORLD"); 
     } 

     private function test_func_NO_PARAMS():void 
     { 
      trace("No params was called successfully!"); 
     } 

     private function test_func_ONE_PARAM(some_number:int):void 
     { 
      trace("One param was called successfully! 'some_number' = " + some_number); 
     } 

     private function test_func_TWO_PARAMS(stringA:String, stringB:String):void 
     { 
      trace("Two params was called successfully! 'stringA' = " + stringA + ", 'stringB' = " + stringB); 
     } 
    } 
} 

所以當你在看您的測試運行前兩個調用工作正常,調用函數不帶參數帶一個參數的那個。

問題是當我需要傳遞多個參數!

解決問題:

  1. 嗯,我知道,如果我可以簡單地保留...args原樣,並把它傳遞到this.m_function.call調用它會得到解決。

  2. 另一種方式,也許是有一些不大不小的foreach循環這將喂指定...args在時機成熟時,再一次的,我怎麼會參考/傳呢?

這裏必須有一個很好的技巧才能使它工作,歡迎您與我一起出汗!

回答

1

嘗試改變這一行:

this.m_function.call(this.m_the_this, this.m_args); 

這樣:

this.m_function.apply(this.m_the_this, this.m_args); 

apply參數傳遞到應用函數作爲列表而不是單個陣列(彷彿你寫相同的效果the_function(arg[0],arg[1],arg[N]))。

編輯:

也許我不理解你的問題,但你的代碼的簡化這個樣本工作正常(我沒有使用定時器和我沒有構建任何實例,但我認爲最主要的這裏是如何應用工程;以參數數組和將它們作爲變量參數列表被調用的函數):

private function arg0():void { 
    trace("arg0"); 
} 

private function arg1(a:*):void { 
    trace("arg1"); 
} 

private function arg2(a:*,b:*):void { 
    trace("arg2"); 
} 

private function arg3(a:*,b:*,c:*):void { 
    trace("arg3"); 
}      

private function test():void { 
    schedule_call(this,arg0,10); 
    schedule_call(this,arg1,10,1); 
    schedule_call(this,arg2,10,1,2); 
    schedule_call(this,arg3,10,1,2,3); 
} 

public function schedule_call(the_this:*, f:Function, interval:int, ...args):void 
{ 
    var m_args:Array = args; 
    f.apply(the_this, m_args); 
} 
+0

不上兩PARAMS功能工作,看到自己。我試圖以某種方式解決問題(順便說一下),將會更新。讓我知道你是否修好了它。 – Poni 2011-02-15 19:25:39

相關問題