如何在 ActionScript 中创建一系列点击?

How to create a sequence of clicks in ActionScript?

我正在创建一个谜题,玩家必须按正确的顺序或顺序点击按钮才能进入下一关(例如场景 2)。我不知道该怎么做。如果有人知道如何在 Action Script 中实现这一点。

谢谢

场景 1:

enter image description here

每个数字都是一个按钮。现在玩家必须按正确的顺序或顺序 (1 2 3 4 5 6 7 8) 打开下一关 (场景 2)

var checkString:String = "";

//Create event listeners and their functions.
btn1.addEventListener(Mouse.CLICK, oneClick);
btn2.addEventListener(Mouse.CLICK, twoClick);
btn3.addEventListener(Mouse.CLICK, threeClick);
btn4.addEventListener(Mouse.CLICK, fourClick);
btn5.addEventListener(Mouse.CLICK, fiveClick);
btn6.addEventListener(Mouse.CLICK, sixClick);
btn7.addEventListener(Mouse.CLICK, sevenClick);
btn8.addEventListener(Mouse.CLICK, eightClick);


function oneClick(evt:Event):void
{
   //In each event listener function, add a letter or 
   //string to the checkString variable.
   checkString += "on";

   //Then, see if the string matches or not.
   check();
}

function twoClick(evt:Event):void
{
   checkString += "tw";
   check();
}

function threeClick(evt:Event):void
{
   checkString += "th";
   check();
}

function fourClick(evt:Event):void
{
   checkString += "fo";
   check();
}

function fiveClick(evt:Event):void
{
   checkString += "fi";
   check();
}

function sixClick(evt:Event):void
{
   checkString += "si";
   check();
}

function sevenClick(evt:Event):void
{
   checkString += "se";
   check();
}

function eightClick(evt:Event):void
{
   checkString += "ei";
   check();
}



//If the proper sequence is one, two, three, four, five, six, seven, eight the string would read "ontwthfofisiseei".
function check():void
{
   if(checkString == "ontwthfofisiseei")
   {
      //Clear the checkString for convenience before going on.
      clearString();
      //CODE TO GO TO NEW FRAME

gotoAndPlay(1, "Scene 3");
   }

}

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

这是我之前使用的代码,但它在侦听器中显示错误,但事实并非如此。工作

回答您更新后的问题:

您的错误很可能是 Mouse.CLICK 应该是 MouseEvent.CLICK

您的另一个错误是告诉您没有名为 "Scene 3"

的场景

假设您在 Flash/Animate.

的时间轴上有 8 个 MovieClip(或按钮)

实现此目的的(许多)方法之一如下:

  1. 为每个按钮指定一个实例名称。为了减少代码,让我们给它们命名 btn + 它们各自的正确订单号 - 所以 btn1btn2btn3

  2. 您需要为每个按钮添加一个点击侦听器,这样当它们被点击时就会发生一些事情。

    你可以这样做 8 次(每个按钮一个):btn1.addEventListener(MouseEvent.CLICK, buttonClick); 但为了使事情更简单,你可以遍历时间轴上的所有对象并将侦听器添加到名称以开头的每个对象"btn":

    var totalBtns:int = 0; //create a var to store how many buttons there are
    //loop through each child of the current timeline
    var i:int = numChildren;
    while(i--){
        //if the child's name starts with 'btn'
        if(getChildAt(i).name.indexOf("btn") == 0){
            //add the click listener
            getChildAt(i).addEventListener(MouseEvent.CLICK, buttonClick,false,0,true);
            totalBtns++; //increase the total buttons variable by 1
        }
    } 
    

    这也意味着如果您 add/remove 按钮

  3. 您需要一种方法来跟踪单击按钮的时间。为此,我们将使用数组。

    var clickArray:Array = []; //this creates a new array
    //When a button is clicked, you add it to this array
    
  4. 让我们创建单击按钮时调用的函数:

    function buttonClick(e:Event):void {
        //add the item that was just clicked (represented by the event's currentTarget property) to the array
        //so if btn1 was just clicked, btn1 would be e.currentTarget
        clickArray.push(e.currentTarget);
    
        //now disable the button so it can't be clicked anymore
        SimpleButton(e.currentTarget).enabled = false;
    
        //check if all button have been clicked
        if(clickArray.length == totalBtns){
    
           //lets go through every item in the array, and see if it's in the right order
           var ctr:int = 0; //a counter to keep track of the expected next number         var i:int = 0; //iterator for the for loops
           for(i=0;i<clickArray.length;i++){
               //lets convert everything after the 3rd character of the name to a number - so for btn1, that would be 1
               if(parseInt(clickArray[i].name.substring(3)) == ctr + 1){
                  ctr++; //increment the counter to the next expected number
               }else{
                  break; //leave the for loop early since a click was out of place
               }
           }
    
           //if the correct order was achieved
           if(ctr == totalBtns){
               nextScene(); //or however you continue
           }else{
               //the correct order was NOT acheived
    
               //make all the buttons clickable again
               for(i=0;i<clickArray.length;i++){
                   SimpleButton(clickArray[i]).enabled = true;
               }
    
               //reset the array
               clickArray = [];
    
               //probably want to tell the user to try again
           }
        }
    }