Reputation: 1210
I have a flash shooter game and after a user shoots a bottle, i play the destroy animation and remove it from the screen. The problem is when the user click too fast, like superman fast it enters on the method twice, no matter what. Does anyone knows how to solve this?
Here is the code:
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();
}
The first thing i do is to disable the object mouse, and it still happens. I only enable it when im gonna show the bottles again.
Upvotes: 1
Views: 689
Reputation: 1129
Well I suppose there are a few ways you can do this but that depends on how you register the mouse click events. If you are calling addEventListener(MouseEvent.Click, ...) to each of your Bullets to eventually get to bottleHasClicked then you can simply remove the listener on the object by calling removeEventListener(...);
If you are using so other method, another simple way to do this is just by adding a boolean to check if the bottle was already clicked (let's call that variable "wasClicked" and the constructor of a Bottle should set it to false). In this case, what you can do is change the code above to something like this :
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();
}
}
It should do the trick.
Upvotes: 0
Reputation: 5434
Don't you have a "playing" variable in the bottle class? You can check against that, just before calling bottleHasClicked(). Also, providing the addListener call would help.
Upvotes: 0
Reputation: 4238
See this post:
If you only have a single element that needs to have the mouse disabled, use the mouseEnabled property. If however you have child elements on a particular object that you want to cascade the disabling of the mouse events to, make sure to set the mouseChildren property as well. This second one has caught me before when I was still getting a response to mouse events even though I though I had disabled them.
Upvotes: 1