How can I capture if the user clicked a wrong button?

83 views Asked by At

I made a game remember for the first fishes. I found a code like this. I also have wrong buttons.

Wrong buttons list:

pinkButton.addEventListener(MouseEvent.CLICK, pinkClick);
whiteButton.addEventListener(MouseEvent.CLICK, whiteClick);
greyButton.addEventListener(MouseEvent.CLICK, greyClick);

And I use this code

import flash.events.MouseEvent;
var checkString:String = "";
 
//Create event listeners and their functions.
YellowButton.addEventListener(MouseEvent.CLICK, yellowClick);
RedButton.addEventListener(MouseEvent.CLICK, redClick);
BlueButton.addEventListener(MouseEvent.CLICK, blueClick);



/*True choices*/ 
function yellowClick(evt:Event):void
{
//In each event listener function, add a letter or 
//string to the checkString variable.
checkString += "y";
//Then, see if the string matches or not.
check();
}
function redClick(evt:Event):void
{
checkString += "r";
check();
}
function blueClick(evt:Event):void
{
checkString += "b";
check();
}
/*True choices*/
 
//If the proper sequence is red, yellow, blue, the string would read "ryb".
function check():void
{
if(checkString == "ryb")
{
 //Clear the checkString for convenience before going on.
  clearString();
  //CODE TO GO TO NEW FRAME
  gotoAndStop(3);
}
else
{
 //Make sure the string is at least 3 characters long.
 if(checkString.length >= 3)
{
clearString();
gotoAndStop(1);
    }   
  }
}
function clearString():void
{
//You will want to have a function for clearing the string.
//This is especially useful if you have a button for "start over."
checkString = "";
}

 

if I click yellow, red, blue it works. How can I make wrong choices? Do I have to write a code for all possibilities? Player has 1 chance. For example if player clicked 2 false and 1 true button,or 2 true and 1 false, this results in a loss for the player.

1

There are 1 answers

7
Neal Davis On

Use an array of values. Like

var correctSequence:Array = ["r", "y", "b"];

then have a incrementing variable to give you control over traversing the array

var arrayPosition:int = 0;

and you need a variable to hold the Boolean value of whether or not there have been any wrong guesses:

var noneWrong:Boolean = true;

Then you could do something like

private function playerNextGuess(e:MouseEvent):void{
    if (e.target._color == correctSequence[arrayPosition] && noneWrong == true){
        arrayPosition++;
        if (arrayPosition == 3){
            playerWin();
            arrayPosition = 0;
        }
    } else {
        // put whatever logic you want for when the guess is wrong
        noneWrong = false;
        arrayPosition++;
        if (arrayPosition == 3){
            playerLose();
            arrayPosition = 0;
        }
    }

This will make it so that 3 guesses are made before the results (right or wrong) are given to the player. But it won't tell the player which were right and which were wrong. If none are wrong, the win function is called. If any are wrong the lose function is called. Is that what you wanted?

Hopefully that gets you going in the right direction. Let me know if anything I wrote isn't abundantly clear.