我正在AS3制作游戏。
我有一个名为“UseBox”的类,用于调度事件。
如果thisThing.buyable not event is dispatch且子视图“Building”可见,其中2个建筑物可见且2个不可见。
如果thisThing.destructible那么事件“升级”是发送,并且子建筑物“建筑物”是可见的,其中4个建筑物可见。
问题是发送“可升级”现在一直在进行。
每次儿童“建筑物”变得可见时,是否可以销毁事件?
Thanx为你的答案,
这是我的代码:
UseBox
public function UseBox(stageRef:Stage, thisThing:Object){
if (thisThing.buyable){
useButton.gotoAndStop("buy");
useButton.addEventListener(MouseEvent.CLICK, buyIt, false, 0, true);
}
if (thisThing.destructible){
this.gotoAndStop("upgrade");
upgradeButton.buttonMode = true;
upgradeButton.addEventListener(MouseEvent.CLICK, upgradeIt, false, 0, true);
}
public function buyIt(e:MouseEvent):void{
boxClicked();
showBatiments();
lookButton.removeEventListener(MouseEvent.CLICK, buyIt);
}
public function upgradeIt(e:MouseEvent):void{
boxClicked();
showBatiments();
stageRef.dispatchEvent(new Event("upgradable"));
lookButton.removeEventListener(MouseEvent.CLICK, buyIt);
}
private function showBatiments():void{
Engine.batiments.visible = true;
}
哦,还有我的buidling.as代码:
poulaillerPlusBtn.visible = false;
poulaillerImpossible.visible = true;
poulaillerBtn.addEventListener(MouseEvent.CLICK, poulaillerConstruction, false, 0, true);
stageRef.addEventListener("upgradable", checkConstruction, false, 0, true);
private function checkConstruction(Event):void{
trace("I've heard upgradable");
poulaillerPlusBtn.visible = true;
poulaillerImpossible.visible = false;
poulaillerPlusBtn.addEventListener(MouseEvent.CLICK, poulaillerAmelioration, false, 0, true);
trace("on a vérifé, tu peux améliorer le batiment");
}
因此,如果听到“升级”事件,则调用函数“checkConstruction”,否则不会。
但是,一旦调度了“升级”事件,它似乎始终保持调度,因此当“建筑物”可见时,总是调用“checkConstruction”功能...
答案 0 :(得分:0)
我不是100%确定这是你正在寻找的东西,但是如果你只想要处理一次事件,那么一旦你通过调用checkConstruction
收到它,你就可以删除它从再次听取事件:
private function checkConstruction(Event):void{
//Here, you would remove the function from listening to any more "upgradable" events being dispatched.
stageRef.removeEventListener("upgradable", checkConstruction);
trace("I've heard upgradable");
poulaillerPlusBtn.visible = true;
poulaillerImpossible.visible = false;
poulaillerPlusBtn.addEventListener(MouseEvent.CLICK, poulaillerAmelioration, false, 0, true);
trace("on a vérifé, tu peux améliorer le batiment");
}