bry*_*bam 24 apache-flex flash events arguments actionscript-3
因为当你使用sql lite时,如果你尝试在同一时刻执行一个函数它会抛出一个错误,我只是试图创建一个函数来检查它是否正在执行,如果它是在10毫秒再试一次,这个确切的函数工作正常如果我不必将任何参数传递给函数,但我很困惑如何将vars传递回它将执行的函数.
我想要做:
timer.addEventListener(TimerEvent.TIMER, saveChat(username, chatBoxText));
Run Code Online (Sandbox Code Playgroud)
但它只允许我这样做:
timer.addEventListener(TimerEvent.TIMER, saveChat);
Run Code Online (Sandbox Code Playgroud)
它给了我这个编译错误:
1067:将void类型的值隐式强制转换为不相关的类型函数
我怎样才能通过这个限制?
这是我得到的:
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();
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*rty 27
侦听器调用的函数只能有一个参数,即触发它的事件.
listener:Function
- 处理事件的侦听器函数.此函数必须接受Event对象作为其唯一参数,并且必须不返回任何内容,如下例所示:
function(evt:Event):void
你可以通过让事件调用的函数调用另一个带有所需参数的函数来解决这个问题:
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.
}
Run Code Online (Sandbox Code Playgroud)
您可以做的另一件事是创建一个自定义事件类,它扩展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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以用定义的属性调度它:
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);
}
Run Code Online (Sandbox Code Playgroud)
并听取它:
addEventListener(CustomEvent.SAVE_CHAT, saveChat);
function saveChat(e:CustomEvent):void
{
trace(e.username + ": " + e.chatBoxText);
// Output: Marty: Custom events are easy.
}
Run Code Online (Sandbox Code Playgroud)
Jas*_*wne 11
实际上,您可以通过使用Actionscript的动态函数构造将其他参数传递给事件侦听器,而无需创建自定义事件.
private function addArguments(method:Function, additionalArguments:Array):Function
{
return function(event:Event):void {method.apply(null, [event].concat(additionalArguments));}
}
Run Code Online (Sandbox Code Playgroud)
在为Alert窗口设置closeHandler时,我们调用addArguments()方法并传入一个数组,该数组包含我们想要传递给closeHandler的所有参数.当Alert窗口关闭时,addHandler()方法将返回我们将调用的函数.
protected function btnOK_clickHandler(event:MouseEvent):void
{
Alert.show("Would you like to reverse the text you just entered?", "", Alert.YES | Alert.NO, null, addArguments(alertCloseHandler, [txtInput.text]), null, Alert.YES);
txtInput.text = "";
}
Run Code Online (Sandbox Code Playgroud)
小智 10
1067:将void类型的值隐式强制转换为不相关的类型函数
适当注意你得到的错误:它说a Function
是一种类型而且addEventListener()
想要它.虽然你的听众返回无效,但它是一个Function
!那么,回到听众呢?
timer.addEventListener(TimerEvent.TIMER, saveChat(username, chatBoxText));
function saveChat(username:String, chatBoxText:String):Function {
return function(e:TimerEvent):void {
// Do everything that uses "e", "chatBoxText" and "username" here!
};
}
Run Code Online (Sandbox Code Playgroud)
很简单.它适用于任何类型的事件.并没有关闭问题.
注意事项removeEventListener()
:
不要试图这样做timer.removeEventListener(TimerEvent.TIMER, saveChat(username, chatBoxText))
.它不会识别您的功能,因为您每次使用时都会在其上传递新功能saveChat()
.相反,只需将其引用转换为变量即可:
var functionSaveChat:Function = saveChat(username, chatBoxText);
timer.addEventListener(TimerEvent.TIMER, functionSaveChat);
//trace(timer.hasEventListener(TimerEvent.TIMER));
timer.removeEventListener(TimerEvent.TIMER, functionSaveChat);
//trace(timer.hasEventListener(TimerEvent.TIMER));
Run Code Online (Sandbox Code Playgroud)