2011-07-25 40 views
1

我有一個Flash射擊遊戲,當用戶拍攝一個瓶子後,我播放銷燬動畫並將其從屏幕上移除。問題是當用戶點擊的速度太快時,就像超人一樣快速進入該方法兩次,無論如何。有誰知道如何解決這個問題?如何在點擊對象後禁用鼠標點擊?

下面是代碼:

public function bottleHasClicked(bottle : BottleBase) : void { 
     bottle.mouseEnabled = false; 
     collectedBottles++; 
     bottlesInRound--; 

     gameSound.getShootSound().playSound(); 
     gameSound.getBottleSound().playSound(); 


     ArrayUtil.removeValueFromArray(elementsInScreenArray, bottle); 
     cleanElementsTimer[bottle].stop(); 
     delete cleanElementsTimer[bottle]; 

     if (bottlesInRound == 0) { 
      stopElementsTimer(); 
      showElementsWaitForSeconds(0.5); 
     } 

     createBulletHole(); 
     bottle.play(); 
    } 

我做的第一件事是禁用對象的鼠標,它仍然會發生。我只有在我要再次展示瓶子時才能啓用它。

+0

你是如何要求這種方法? –

回答

1

this post

如果你只需要擁有鼠標 禁用單個元素,使用mouseEnabled屬性。但是,如果您想要將鼠標事件級聯到禁用的 特定對象上的子元素爲 ,請確保將mouseChildren屬性設置爲 。第二個發現之前,我仍然對 響應鼠標事件感興趣,儘管我已經禁用了它們。

+0

我試圖這樣的事情: \t \t \t爲(VAR I:編號= 0; I Gilson

0

難道你在瓶子類中沒有「玩」變量嗎?你可以在調用bottleHasClicked()之前檢查它。另外,提供addListener調用會有所幫助。

0

嗯,我想有幾種方法可以做到這一點,但這取決於你如何註冊鼠標點擊事件。如果您要爲每個Bullets調用addEventListener(MouseEvent.Click,...)以最終到達bottleHasClicked,那麼您可以通過調用removeEventListener(...)來簡單地移除對象上的偵聽器;

如果您使用的是其他方法,另一個簡單的方法是通過添加一個布爾值來檢查瓶子是否已經被點擊(讓我們將該變量稱爲「wasClicked」,並且Bottle的構造函數應該將其設置爲假)。在這種情況下,你可以做的是將上面的代碼更改爲如下所示:

public function bottleHasClicked(bottle : BottleBase) : void 
{ 
    if(!bottle.wasClicked) 
    { 
     bottle.wasClicked = true; 
     bottle.mouseEnabled = false; 
     collectedBottles++; 
     bottlesInRound--; 

     gameSound.getShootSound().playSound(); 
     gameSound.getBottleSound().playSound(); 


     ArrayUtil.removeValueFromArray(elementsInScreenArray, bottle); 
     cleanElementsTimer[bottle].stop(); 
     delete cleanElementsTimer[bottle]; 

     if (bottlesInRound == 0) { 
      stopElementsTimer(); 
      showElementsWaitForSeconds(0.5); 
     } 

     createBulletHole(); 
     bottle.play(); 
    } 
} 

它應該這樣做。