如何将参数传递到事件监听器功能的柔性/动作?监听器、柔性、如何将、参数

2023-09-08 12:01:21 作者:讨你欢

使用SQL精简版,如果你尝试做一个函数在它抛出一个错误,我只是试图让一个函数,将检查其执行相同的时刻,如果是在10毫秒再试一次,这个确切时,由于功能工作正常,如果我没有通过任何函数的自变量,但即时通讯困惑我怎么能通过瓦尔放回它会被执行功能。

我想要做的:

  timer.addEventListener(TimerEvent.TIMER,saveChat(用户名,chatBoxText));
 

但是,这将只允许我这样做:

  timer.addEventListener(TimerEvent.TIMER,saveChat);
 

它给了我这个编译错误:

  

1067 隐含的价值胁迫   类型void一个不相关的类型   功能

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

下面是我有:

 公共职能saveChat(用户名:字符串,chatBoxText:字符串,E:TimerEvent = NULL):无效
{
    VAR定时器:定时器=新的定时器(10,1);
    timer.addEventListener(TimerEvent.TIMER,saveChat);

    如果(!saveChatSql.executing)
    {
        saveChatSql.text =UPDATE active_chats SET康沃='+ chatBoxText +'其中username ='+用户名+;;
        saveChatSql.execute();
    }
    别的timer.start();
}
 
用了多年的苹果手机才知道,原来这个耳朵图标是个监听器

解决方案

所谓由听众只能有一个参数的函数,是事件触发的。

  

监听器:功能 - 的侦听器函数处理事件。   此函数必须接受Event   对象作为其唯一的参数,并且不能   返回任何结果,如下面的示例   说明:

     

函数(EVT:事件):无效

     

Source

您可以通过让调用该事件的函数调用另一个函数所需参数解决这个问题:

  timer.addEventListener(TimerEvent.TIMER,_saveChat);
功能_saveChat(E:TimerEvent):无效
{
    saveChat(阿根廷,阿根廷,阿根廷);
}

功能saveChat(ARG1:类型,ARG2:类型,ARG3:型):空白
{
    //你的逻辑。
}
 

您可以做的另一件事创建可扩展的自定义事件类对象类型:flash.events.Event ,并创建你需要在属性。

 包
{
    进口对象类型:flash.events.Event;

    公共类自定义事件扩展事件
    {
        //你的自定义事件类型。
        公共静态常量SAVE_CHAT:字符串=saveChat;

        //你的自定义属性。
        公共变种用户名:字符串;
        公共变种chatBoxText:字符串;

        //构造函数。
        公共功能自定义事件(类型:字符串,气泡:布尔=假,取消:布尔=假):无效
        {
            超(类型,泡沫,取消);
        }
    }
}
 

然后就可以使用性能调度此定义的:

  timer.addEventListener(TimerEvent.TIMER,_saveChat);
功能_saveChat(E:TimerEvent):无效
{
    VAR EVT:自定义事件=新的自定义事件(CustomEvent.SAVE_CHAT);

    evt.username =君子好逑;
    evt.chatBoxText =自定义事件是容易的。

    则dispatchEvent(EVT);
}
 

和听吧:

 的addEventListener(CustomEvent.SAVE_CHAT,saveChat);
功能saveChat(五:自定义事件):无效
{
    跟踪(e.username +:+ e.chatBoxText);
    //输出:马蒂:自定义事件很容易。
}
 

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.

I want to do:

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

But it will only allow me to do:

timer.addEventListener(TimerEvent.TIMER, saveChat);

It gives me this compile error:

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

How can I get this to pass this limitation?

Here's what I've got:

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 — 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

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

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

And listen for it:

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