在AS3的比赛条件射击事件 [英] Race condition firing events in AS3
问题描述
我有一些麻烦,以正确的纪事订单发射和移除事件。以下代码提供以下输出:
- 将海报保存到数据库中,并发送事件
- 调用服务,调度事件删除= false
- 调用服务,dispatch event removed = false
- 调用服务,dispatch event removed = true
- 将海报保存到数据库中,并发送事件
- 将海报保存到数据库中,并发送事件
当然这应该是这样的:
- 将海报保存到数据库中,并发送事件
- 调用服务,dispatch事件删除= true
- 将海报保存到数据库中,并发送事件
- 调用服务,删除事件= true
- 将海报保存到数据库中,并发送事件
- 呼叫服务,dispatch event removed = true
有人可以帮我吗?我不知道如何解决这个问题。
thx!
for(var i:int = 0; i< 3; i ++){
createPoster();
}
函数createPoster(){
Main.db.savePoster();
Main.db.addEventListener(Config.evt_SAVEPOSTER_READY,callService);
}
函数callService(){
Main.db.removeEventListener(Config.evt_SAVEPOSTER_READY,callService);
}
问题是你是在单个中注册相同的函数
对象 callService
同一事件 Config.evt_SAVEPOSTER_READY
EvenDispatcher db
。所以一旦savePoster在成功保存海报之后调度事件,db接收到事件,并且调用了三个eventHandler(在这种情况下叫做callService),因为callService被注册了三次。所以一个解决方案是从海报派发事件。
for(var i:int = 0; i< 3; i ++){
createPoster();
}
函数createPoster(){
poster = Main.db.savePoster();
poster.addEventListener(Config.evt_SAVEPOSTER_READY,callService);
}
函数callService(e:PosterEvent){
e.target.removeEventListener(Config.evt_SAVEPOSTER_READY,callService);
}
I have some troubles firing and removing events in the right chronicle order. The code below gives the following output:
- save poster into db, and dispatch event
- calling service, dispatch event removed = false
- calling service, dispatch event removed = false
- calling service, dispatch event removed = true
- save poster into db, and dispatch event
- save poster into db, and dispatch event
of course this should be more something like:
- save poster into db, and dispatch event
- calling service, dispatch event removed = true
- save poster into db, and dispatch event
- calling service, dispatch event removed = true
- save poster into db, and dispatch event
- calling service, dispatch event removed = true
Can someone help me with this? I'm running out of ideas on how to tackle this.
thx!
for(var i:int = 0;i< 3;i++){
createPoster();
}
function createPoster(){
Main.db.savePoster();
Main.db.addEventListener(Config.evt_SAVEPOSTER_READY, callService);
}
function callService(){
Main.db.removeEventListener(Config.evt_SAVEPOSTER_READY, callService);
}
The problem is that you are registering same function callService
for same event Config.evt_SAVEPOSTER_READY
on single EvenDispatcher
objectdb
. So as soon first savePoster dispatches the event after successfully saving the poster, db receives the event and three eventHandlers (in this case callService) are called because callService is registered thrice. So one solution would be dispatching the events from Poster.
for(var i:int = 0;i< 3;i++){
createPoster();
}
function createPoster(){
poster = Main.db.savePoster();
poster.addEventListener(Config.evt_SAVEPOSTER_READY, callService);
}
function callService(e:PosterEvent){
e.target.removeEventListener(Config.evt_SAVEPOSTER_READY, callService);
}
这篇关于在AS3的比赛条件射击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!