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

时间:2017-11-17 20:51:59

标签: actionscript-3 flash

我正在制作一个拼图,其中玩家必须点击正确顺序或序列上的按钮才能进入下一级别(例如场景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 = "";
}

这是我之前使用的代码,但是它在监听器中显示错误而且它没有。工作

1 个答案:

答案 0 :(得分:1)

要回答您更新的问题:

您的错误可能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
        }
    } 
    

    如果您添加/删除按钮

  3. ,这也意味着以后的工作量会减少
  4. 您需要一种方法来跟踪点击按钮的时间。为此,我们将使用数组。

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

    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
           }
        }
    }