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

因为当使用sql lite如果你尝试在同一时刻执行一个功能,它会抛出一个错误,我只是试图做一个函数,将检查它是否执行,如果它是在10毫秒再次尝试,这个确切的功能工作正常如果我不必传递任何参数到函数,但我困惑我如何可以将vars传递回它将要执行的函数

我想要做

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();
}

解决方法

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

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

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

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.
}

相关文章

一:display:flex布局display:flex是一种布局方式。它即可以...
1. flex设置元素垂直居中对齐在之前的一篇文章中记载过如何...
移动端开发知识点pc端软件和移动端apppc端软件是什么,有哪些...
最近挺忙的,准备考试,还有其他的事,没时间研究东西,快周...
display:flex;把容器设置为弹性盒模型(设置为弹性盒模型之后...
我在网页上运行了一个Flex应用程序,我想使用Command←组合键...