问题描述
我有一些麻烦射击和消除在正确的编年史顺序事件。下面的code给出了下面的输出:
- 在保存的海报到数据库,并调度事件
- 呼叫服务,调度事件删除= FALSE
- 呼叫服务,调度事件删除= FALSE
- 呼叫服务,调度事件删除= TRUE
- 在保存的海报到数据库,并调度事件
- 在保存的海报到数据库,并调度事件
这应该是更多的是这样的:
- 在保存的海报到数据库,并调度事件
- 呼叫服务,调度事件删除= TRUE
- 在保存的海报到数据库,并调度事件
- 呼叫服务,调度事件删除= TRUE
- 在保存的海报到数据库,并调度事件
- 呼叫服务,调度事件删除= TRUE
有人可以帮助我?我跑出来就想法如何解决这个问题。
THX!
的(VAR我:= 0;我3;;我++){
createPoster();
}
功能createPoster(){
Main.db.savePoster();
Main.db.addEventListener(Config.evt_SAVEPOSTER_READY,callService);
}
功能callService(){
Main.db.removeEventListener(Config.evt_SAVEPOSTER_READY,callService);
}
现在的问题是,你正在注册相同的功能 callService
对于同一事件配置.evt_SAVEPOSTER_READY
单 EvenDispatcher
对象分贝
。因此,一旦第一savePoster分派成功保存海报后,分贝接收事件和三个事件处理器(在此情况下callService)称为因为callService被登记三次该事件。所以,一个解决办法是从海报调度事件
的(VAR我:= 0;我3;;我++){
createPoster();
}
功能createPoster(){
海报= 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竞争条件触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!