-1

当以特定顺序单击按钮时,我正在努力使 3 个按钮能够进入一个框架。例如,按钮标记为
按钮 1
按钮 2
按钮 3

并且需要单击它们的顺序是按钮 3、按钮 1、按钮 2 才能进入下一帧,我该如何实现?

这就是我到目前为止所拥有的。

 var clicked = MouseEvent.CLICK 
 var buttons = new Array(YellowButton, BlueButton, RedButton); 
 for (var a=0; a<buttons.lenght; a++) 
 { 
   buttons[a].buttonMode=true 
   buttons[a].addEventListener(clicked,RedYellowBlue); 
 } 

 function RedYellowBlue(event:MouseEvent):void { gotoAndStop(20); }

提前感谢您的帮助

4

3 回答 3

0

//这里是代码

var buttons:Array = new Array(YellowButton, BlueButton, RedButton);



for(var i:int=0;i< buttons.length;i++) 
{
  buttons[a].buttonMode=true 
  buttons[a].addEventListener(MouseEvent.CLICK,buttonsClickHandler); 
} 

function buttonsClickHandler(event:MouseEvent):void { 

    var button:MovieClip = MovieClip(event.target);

    switch(button.name)
    {
        case "YellowButton":
        case "BlueButton":
        gotoAndStop(2);
        break;
        case "RedButton":
        gotoAndStop(3);
        break;
    }
}
于 2012-10-31T17:25:23.743 回答
0

我建议创建一个字符串对象,该对象将通过单击按钮进行组合,然后与您设置的特定序列进行比较。这样做的好处是你可以让它适用于任何长度的序列。

这里最重要的是,您不希望所有三个按钮都使用相同的事件侦听器。您希望他们的操作是唯一的,否则,单击一个与单击所有其他操作相同(这是您当前代码的问题。)

var checkString:String = "";

//Create event listeners and their functions.
YellowButton.addEventListener(Mouse.CLICK, yellowClick);
RedButton.addEventListener(Mouse.CLICK, redClick);
BlueButton.addEventListener(Mouse.CLICK, blueClick);

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();
}

//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(20);
   }
   else
   {
      //Make sure the string is at least 3 characters long.
      if(checkString.length >= 3)
      {
         clearString();
         gotoAndStop(foo);
      }
   }
}

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

这应该足以让你开始。如果您需要其他帮助,请随时评论我的回答。

于 2012-10-31T16:25:24.827 回答
0

要合并@JasonMc92 的答案,您可以使用目标名称作为checkString字母,因此您只需为所有按钮调用一个函数。

yellowButton.addEventListener(MouseEvent.CLICK, redYellowBlue);
redButton.addEventListener(MouseEvent.CLICK, redYellowBlue);
blueButton.addEventListener(MouseEvent.CLICK, redYellowBlue);

function redYellowBlue(e:MouseEvent){
  checkString += e.currentTarget.name.substr(0,1);
  check();
}
//... continue on with Jason's functions

或者,由于您已将它们设置在一个数组中,您也可以只使用按钮的索引作为您的检查序列。就像是:

function redYellowBlue(e:MouseEvent){
  checkString += String(buttons.indexOf(e.currentTarget));
  check();
}

function check(){
  if (checkString == '201') {
    // ... handle correct sequence
  }
}

此外,只是一个最佳实践提示:

类名只能使用前导大写。实例名称和函数名称应以小写字母开头。(领先的上限不会破坏任何东西,但这是标准做法)

于 2012-10-31T16:44:20.273 回答