如何将参数传递给flex / actionscript中的事件监听器函数? [英] How to pass arguments into event listener function in flex/actionscript?

查看:117
本文介绍了如何将参数传递给flex / actionscript中的事件监听器函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想要做:

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

但只允许我这样做:



timer.addEventListener(TimerEvent.TIMER,saveChat);

  

它给我这个编译错误:


1067:将值
的隐式强制类型为无关类型的空值
函数


我如何得到这个限制?



这是我所得到的:

  public function saveChat(username:String,chatBoxText:String,e:TimerEvent = null):void 
{
var timer:Timer = new Timer(10,1);
timer.addEventListener(TimerEvent.TIMER,saveChat);

如果(!saveChatSql.executing)
{
saveChatSql.text =UPDATE active_chats SET convo ='+ chatBoxText +'WHERE username ='+ username +'; ;
saveChatSql.execute();
}
else timer.start();
}


解决方案

只能有一个参数,这是触发它的事件。


listener:Function - 处理事件的侦听器函数。
此函数必须接受一个Event
对象作为其唯一的参数,并且必须
不返回,因为此示例
显示:



function(evt:Event):void




您可以通过将事件调用的函数调用另一个具有必需参数的函数来实现:



<$ timer.addEventListener(TimerEvent.TIMER,_saveChat); p $ p>
函数_saveChat(e:TimerEvent):void
{
saveChat(arg,arg,arg);
}

函数saveChat(arg1:type,arg2:type,arg3:type):void
{
//您的逻辑。
}

另一件事你可以创建一个扩展 flash.events.Event 并创建您需要的属性。

  package 
{
import flash.events.Event;

public class CustomEvent extends Event
{
//您的自定义事件类型。
public static const SAVE_CHAT:String =saveChat;

//您的自定义属性。
public var username:String;
public var chatBoxText:String;

//构造函数。
public function CustomEvent(type:String,bubbles:Boolean = false,cancelable:Boolean = false):void
{
super(type,bubbles,cancelable);
}
}
}

然后你可以发送这个定义的属性:

  timer.addEventListener(TimerEvent.TIMER,_saveChat); 
函数_saveChat(e:TimerEvent):void
{
var evt:CustomEvent = new CustomEvent(CustomEvent.SAVE_CHAT);

evt.username =Marty;
evt.chatBoxText =自定义事件很容易。

dispatchEvent(evt);
}

并收听:

  addEventListener(CustomEvent.SAVE_CHAT,saveChat); 
函数saveChat(e:CustomEvent):void
{
trace(e.username +:+ e.chatBoxText);
//输出:Marty:自定义事件很简单。
}


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

这篇关于如何将参数传递给flex / actionscript中的事件监听器函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆