微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/flex/174448.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐