2012-06-08 91 views
1

是否有一個動作庫提供排隊系統?
這個系統就必須讓我傳遞對象,我想調用它,參數的功能,是這樣的:排隊系統的動作

Queue.push(Object, function_to_invoke, array_of_arguments) 

或者,是否有可能(去)序列化的函數調用?我如何用給定的參數來評估'function_to_invoke'?

在此先感謝您的幫助。

回答

2

有在ActionScript 3.0沒有具體的隊列或堆棧型數據提供的結構,但你可能會能夠找到一個圖書館(CasaLib也許),提供了一些沿着這些線。

下面的代碼片段應該適合你,但你應該知道,因爲它通過字符串引用函數名,所以如果引用不正確,你將不會得到任何有用的編譯器錯誤。

該示例使用rest parameter,它允許您指定任意長度的數組作爲您的方法的參數。

function test(... args):void 
{ 
    trace(args); 
} 

var queue:Array = []; 
queue.push({target: this, func: "test", args: [1, 2, "hello world"] }); 
queue.push({target: this, func: "test", args: ["apple", "pear", "hello world"] }); 

for (var i:int = 0; i < queue.length; i ++) 
{ 
    var queued:Object = queue[i]; 
    queued.target[queued.func].apply(null, queued.args); 
} 
+0

這正是我所期待的。太感謝了。 – Bastien

1

當然,這給JavaScript

const name:String = 'addChild' 
    , container:Sprite = new Sprite() 
    , method:Function = container.hasOwnProperty(name) ? container[name] : null 
    , child:Sprite = new Sprite(); 
if (method) 
    method.apply(this, [child]); 

的工作原理類似這樣的查詢方法可能看上去像:

function queryFor(name:String, scope:*, args:Array = null):void 
{ 
    const method:Function = scope && name && scope.hasOwnProperty(name) ? scope[name] : null 
    if (method) 
     method.apply(this, args); 
}