2011-06-20 23 views
24

因爲當使用sql lite如果你嘗試並在同一時間做一個函數它會引發一個錯誤,即時只是試圖做一個函數,將檢查它的執行,如果它是在10毫秒內再次嘗試,這個確切的函數工作正常,如果我不必傳遞任何參數的功能,但我很困惑,我可以通過瓦爾回到它將執行的功能。如何將參數傳入flex/actionscript中的事件偵聽器函數?

我想做的事:

timer.addEventListener(TimerEvent.TIMER, saveChat(username, chatBoxText)); 

但只會讓我做:

timer.addEventListener(TimerEvent.TIMER, saveChat); 

它給了我這個編譯錯誤:

1067: Implicit coercion of a value of type void to an unrelated type Function

我怎樣才能得到這是爲了通過這個限制?

下面是我得到了什麼:

public function saveChat(username:String, chatBoxText:String, e:TimerEvent=null):void 
{ 
    var timer:Timer = new Timer(10, 1); 
    timer.addEventListener(TimerEvent.TIMER, saveChat); 

    if(!saveChatSql.executing) 
    { 
     saveChatSql.text = "UPDATE active_chats SET convo = '"+chatBoxText+"' WHERE username = '"+username+"';"; 
     saveChatSql.execute(); 
    } 
    else timer.start(); 
} 

回答

26

偵聽器調用只能有一個說法,這是它的觸發事件的函數。

listener:Function — The listener function that processes the event. This function must accept an Event object as its only parameter and must return nothing, as this example shows:

function(evt:Event):void

Source

您可以通過具有由事件調用的函數調用另外一個函數所需參數解決這個問題:

timer.addEventListener(TimerEvent.TIMER, _saveChat); 
function _saveChat(e:TimerEvent):void 
{ 
    saveChat(arg, arg, arg); 
} 

function saveChat(arg1:type, arg2:type, arg3:type):void 
{ 
    // Your logic. 
} 

你可以做創建擴展flash.events.Event自定義事件類的另一件事情,並創建您需要的屬性。

package 
{ 
    import flash.events.Event; 

    public class CustomEvent extends Event 
    { 
     // Your custom event 'types'. 
     public static const SAVE_CHAT:String = "saveChat"; 

     // Your custom properties. 
     public var username:String; 
     public var chatBoxText:String; 

     // Constructor. 
     public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false):void 
     { 
      super(type, bubbles, cancelable); 
     } 
    } 
} 

然後你就可以用定義的屬性調度此:

timer.addEventListener(TimerEvent.TIMER, _saveChat); 
function _saveChat(e:TimerEvent):void 
{ 
    var evt:CustomEvent = new CustomEvent(CustomEvent.SAVE_CHAT); 

    evt.username = "Marty"; 
    evt.chatBoxText = "Custom events are easy."; 

    dispatchEvent(evt); 
} 

,並聆聽它:

addEventListener(CustomEvent.SAVE_CHAT, saveChat); 
function saveChat(e:CustomEvent):void 
{ 
    trace(e.username + ": " + e.chatBoxText); 
    // Output: Marty: Custom events are easy. 
} 
1

你可以試試這個:

var timerHandler:Function = function (event:TimerEvent):void 
{ 
    saveChat(username,chatBoxText,event); 
} 

timer.addEventListener(TimerEvent.TIMER, timerHandler); 
12

其實,你可以其他參數傳遞給事件偵聽器,而無需創建通過使用Actionscript的動態函數構造的自定義事件。

private function addArguments(method:Function, additionalArguments:Array):Function 
{ 
    return function(event:Event):void {method.apply(null, [event].concat(additionalArguments));} 
} 

當警報窗口建立是closeHandler我們稱之爲addArguments()方法,並傳遞在陣列continaing所有我們想傳遞給是closeHandler參數。當Alert窗口關閉時,addHandler()方法將返回我們將調用的函數,幷包含參數。

protected function btnOK_clickHandler(event:MouseEvent):void 
{ 
    Alert.show("Would you like to reverse the text you just entered?", "", Alert.YES | Alert.NO, null, addArguments(alertCloseHandler, [txtInput.text]), null, Alert.YES); 
    txtInput.text = ""; 
} 
+0

鏈接無法工作 – Kukeltje

1

以上調用saveChat(阿根廷,阿根廷,ARG)它不適合我,我需要傳遞的是我沒有在這個方法的參數,但我有另一種解決方案

我總是用另一種方法passParameters到添加新的論據:

public function passParameters(method:Function,additionalArguments:Array):Function 
{return function(event:Event):void{ 
    method.apply(null, [event].concat(additionalArguments));} 
} 

的解釋是在這裏 - 它的簡單,總是工作 http://sinfinity.pl/blog/2012/03/28/adding-parameters-to-event-listener-in-flex-air-as3/

11

1067: Implicit coercion of a value of type void to an unrelated type Function

請注意您得到的錯誤:它表示Function是一種類型,addEventListener()需要它。雖然你的聽衆無效,但它是一個Function!那麼,怎麼樣返回聽衆

timer.addEventListener(TimerEvent.TIMER, saveChat(username, chatBoxText)); 
function saveChat(username:String, chatBoxText:String):Function { 
    return function(e:TimerEvent):void { 
    // Do everything that uses "e", "chatBoxText" and "username" here! 
    }; 
} 

這樣簡單。它適用於任何類型的事件。 而且沒有關閉問題。在removeEventListener()

注:

不要試圖做timer.removeEventListener(TimerEvent.TIMER, saveChat(username, chatBoxText))。它不會識別您的功能,因爲您每次使用saveChat()時都會傳遞一個新的功能。相反,只要把它的參考變成一個變量,你就完成了:

var functionSaveChat:Function = saveChat(username, chatBoxText); 
timer.addEventListener(TimerEvent.TIMER, functionSaveChat); 
//trace(timer.hasEventListener(TimerEvent.TIMER)); 
timer.removeEventListener(TimerEvent.TIMER, functionSaveChat); 
//trace(timer.hasEventListener(TimerEvent.TIMER)); 
+0

哇,這真的很有幫助。謝謝!我從來沒有看到這種可能性評論。使用它有什麼缺點嗎?也許有一些內存泄漏或類似的東西? – OMA

+0

@OMA我已經很好很多年了。如果你想確定,用[內存分析器]測試它(http://stackoverflow.com/questions/1872687/actionscript-3-profiler-memory-analysis-tool)。 – 2013-03-19 02:24:16

+0

太好了,謝謝。在我的情況下,instaed將變量中的引用保存到變量中,就像你所建議的那樣,我使用event.currentTarget.removeEventListener(MyListener,arguments ['callee']);從函數內部刪除偵聽器。 – OMA

相關問題