actionscript-3 – 如何将参数传递给flex/actionscript中的事件监听器函数?

前端之家收集整理的这篇文章主要介绍了actionscript-3 – 如何将参数传递给flex/actionscript中的事件监听器函数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
因为当使用sql lite如果你尝试在同一时刻执行一个功能,它会抛出一个错误,我只是试图做一个函数,将检查它是否执行,如果它是在10毫秒再次尝试,这个确切的功能工作正常如果我不必传递任何参数到函数,但我困惑我如何可以将vars传递回它将要执行的函数

我想要做:

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

但这只会让我做:

  1. timer.addEventListener(TimerEvent.TIMER,saveChat);

它给我这个编译错误

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

我如何得到这个通过这个限制?

这里是我有:

  1. public function saveChat(username:String,chatBoxText:String,e:TimerEvent=null):void
  2. {
  3. var timer:Timer = new Timer(10,1);
  4. timer.addEventListener(TimerEvent.TIMER,saveChat);
  5.  
  6. if(!saveChatsql.executing)
  7. {
  8. saveChatsql.text = "UPDATE active_chats SET convo = '"+chatBoxText+"' WHERE username = '"+username+"';";
  9. saveChatsql.execute();
  10. }
  11. else timer.start();
  12. }

解决方法

一个侦听器调用函数只能有一个参数,它是触发它的事件。

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

07000

您可以通过将事件调用函数调用另一个具有必需参数的函数解决此问题:

  1. timer.addEventListener(TimerEvent.TIMER,_saveChat);
  2. function _saveChat(e:TimerEvent):void
  3. {
  4. saveChat(arg,arg,arg);
  5. }
  6.  
  7. function saveChat(arg1:type,arg2:type,arg3:type):void
  8. {
  9. // Your logic.
  10. }

另一件事你可以创建一个自定义事件类,扩展flash.events.Event并创建您需要的属性

  1. package
  2. {
  3. import flash.events.Event;
  4.  
  5. public class CustomEvent extends Event
  6. {
  7. // Your custom event 'types'.
  8. public static const SAVE_CHAT:String = "saveChat";
  9.  
  10. // Your custom properties.
  11. public var username:String;
  12. public var chatBoxText:String;
  13.  
  14. // Constructor.
  15. public function CustomEvent(type:String,bubbles:Boolean=false,cancelable:Boolean=false):void
  16. {
  17. super(type,bubbles,cancelable);
  18. }
  19. }
  20. }

然后您可以使用定义的属性调度它:

  1. timer.addEventListener(TimerEvent.TIMER,_saveChat);
  2. function _saveChat(e:TimerEvent):void
  3. {
  4. var evt:CustomEvent = new CustomEvent(CustomEvent.SAVE_CHAT);
  5.  
  6. evt.username = "Marty";
  7. evt.chatBoxText = "Custom events are easy.";
  8.  
  9. dispatchEvent(evt);
  10. }

并听:

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

猜你在找的Flex相关文章