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);
}
}
}
所以當你在看您的測試運行前兩個調用工作正常,調用函數不帶參數和帶一個參數的那個。
問題是當我需要傳遞多個參數!
解決問題:
嗯,我知道,如果我可以簡單地保留的
...args
原樣,並把它傳遞到this.m_function.call
調用它會得到解決。另一種方式,也許是有一些不大不小的
foreach
循環這將喂指定...args
在時機成熟時,再一次的,我怎麼會參考/傳呢?
這裏必須有一個很好的技巧才能使它工作,歡迎您與我一起出汗!
不上兩PARAMS功能工作,看到自己。我試圖以某種方式解決問題(順便說一下),將會更新。讓我知道你是否修好了它。 – Poni 2011-02-15 19:25:39