如何将参数传递给 flex/actionscript 中的事件侦听器函数? [英] How to pass arguments into event listener function in flex/actionscript?
问题描述
因为在使用 sql lite 时,如果您同时尝试执行一个函数会引发错误,我只是尝试创建一个函数来检查它是否正在执行,如果在 10 毫秒内再次尝试,这正是如果我不必将任何参数传递给函数,函数就可以正常工作,但我很困惑如何将变量传递回它将执行的函数.
Since when using sql lite if you try and do a function at the same moment it throws an error, im just trying to make a function that will check if its executing, and if it is try again in 10 milliseconds, this exact function works fine if i dont have to pass any arguments to the function but im confused how I can pass the vars back into the function it'll be executing.
我想做:
timer.addEventListener(TimerEvent.TIMER, saveChat(username, chatBoxText));
但它只会让我做:
timer.addEventListener(TimerEvent.TIMER, saveChat);
它给了我这个编译错误:
It gives me this compile error:
1067: 值的隐式强制将 void 输入到不相关的类型功能
1067: Implicit coercion of a value of type void to an unrelated type Function
我怎样才能让它通过这个限制?
How can I get this to pass this limitation?
这是我所拥有的:
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();
}
推荐答案
被监听器调用的函数只能有一个参数,即触发它的事件.
A function called by a listener can only have one argument, which is the event triggering it.
listener:Function
— 处理事件的侦听器函数.这个函数必须接受一个事件对象作为其唯一参数并且必须什么都不返回,就像这个例子显示:
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
您可以通过让事件调用的函数调用另一个具有所需参数的函数来解决此问题:
You can get around this by having the function called by the event call another function with the required arguments:
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
并在其中创建您需要的属性.
Another thing you can do create a custom event class that extends flash.events.Event
and create properties that you need within.
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);
}
}
}
然后你可以使用定义的属性来发送它:
Then you can dispatch this with properties defined:
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.
}
这篇关于如何将参数传递给 flex/actionscript 中的事件侦听器函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!