监听自定义事件时要添加什么事件监听器?
当 sql 查询返回不匹配项时,我发送一个事件,以便我可以继续添加到数据库。看起来动作脚本要求我将侦听器附加到某些东西,但我实际上没有任何看起来像的变量就像我在代码中的逻辑候选人一样。
我只想监听 isNewRecord 事件被调用,以便我可以运行插入查询;现在它说调用 addEventListern 和dispatchEvent 可能未定义的方法
public function addBG(BG:Number, datetime:String, batch:Boolean = false):void{
checkRecord('Gb', datetime, matchRecord);
addEventListener("isNewRecord", recordExists);
function recordExists()
{/*code to execute query*/}
public function matchRecord(result:SQLResult):void {
var match:String = result.data[0];
if (match == null) {
var allClear:Event = new Event("isNewRecord");
dispatchEvent(allClear);
}
}
I am sending an event when an sql query returns no matches so that I can continue with the addition to the database.. It seems like actionscript is requiring that I attach the listener to something, but I don't really have any variables that seem like logical candidates at the point where I am in my code.
I just want to listen for the isNewRecord event to be called so that I can then run the insert query; right now it's saying call to possibly undefined method for addEventListern and for dispatchEvent
public function addBG(BG:Number, datetime:String, batch:Boolean = false):void{
checkRecord('Gb', datetime, matchRecord);
addEventListener("isNewRecord", recordExists);
function recordExists()
{/*code to execute query*/}
public function matchRecord(result:SQLResult):void {
var match:String = result.data[0];
if (match == null) {
var allClear:Event = new Event("isNewRecord");
dispatchEvent(allClear);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的代码有问题。你有一个函数中的函数。
另外,您的代码是否扩展了 EventDispatcher 类(或扩展它的任何类,如 Sprite、MovieClip 等?)请确保它是。
试试这个:
Your code is buggy. You have a function within a function.
Also, is your code extending EventDispatcher class (or any class that extends it, like Sprite, MovieClip, etc.?) Make sure it is.
Try this:
您不需要使用事件。您对 SQLResult 的处理似乎是同步的,没有由于与用户、服务器或任何可能需要一些时间的交互而导致的延迟。
当 Flash 执行您的代码时,它会执行以下操作:
在添加侦听器之前调度事件。
这是你应该做的:
干杯
You don't need to use events. Your processing of the SQLResult seems synchronous, there is no latency due to any interaction with the user, the server or anything that may take some time.
When Flash executes your code it does the folowing:
The event is dispatched before the listener is added.
Here is what you should do:
Cheers