如果用户点击了错误的按钮,我该如何捕获?

时间:2017-01-09 13:42:54

标签: actionscript-3 flash-cs6

我记得第一条鱼的游戏。我找到了这样的代码。我也有错误的按钮。

错误的按钮列表:

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

我使用此代码

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 = "";
}

如果我点击黄色,红色,蓝色就可以了。我怎么能做出错误的选择?我是否必须为所有可能性编写代码?玩家有一次机会。例如,如果玩家点击了2个假和1个真实按钮,或者2个真实和1个假,则会导致玩家丢失。

1 个答案:

答案 0 :(得分:1)

使用值数组。喜欢

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

然后有一个递增变量,让你可以控制遍历数组

var arrayPosition:int = 0;

你需要一个变量来保存布尔值是否有任何错误的猜测:

var noneWrong:Boolean = true;

然后你可以做类似

的事情
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;
        }
    }

这将使得在向玩家提供结果(正确或错误)之前进行3次猜测。但它不会告诉玩家哪些是正确的,哪些是错的。如果没有错,则调用win函数。如果有任何错误,则调用lost函数。那是你想要的吗?

希望这会让你朝着正确的方向前进。如果我写的任何内容都不清楚,请告诉我。