3 Replies Latest reply on Aug 29, 2009 10:07 PM by Rothrock

    Newbie actionscript question

    Robert Luciani

      Hi there,


      Here is the scenario,


      I am designing a simple game in actionscript 2.0 and I have 9 buttons on my screen.

      I want it so that if a certain combination of 4 buttons are pressed then a nested movie (correct.swf) plays. If it's the wrong buttons pressed then a different nested movie plays (incorrect.swf)


      any ideas?


      Thank you in advance!

        • 1. Re: Newbie actionscript question
          Ned Murphy Adobe Community Professional & MVP

          You need to store something relating to the sequence of the buttons as they are pressed, which you can easily do as an array or as a string.  Each time a button is pressed it appends something related to that specific button to the array/string, and once the length of the array/string equals 4 you test it against whatever sequence it is supposed to match.


          As an example, let's say you have a 4 digit number you are trying to match.  Each button could be designated to represent a unique value of 1 thru 9.


          var matchNum:String = "1234";

          var pressNum:String;


          btn1.onPress = function(){

               pressNum += "1";




          function testNum(){

               if(pressNum.length == 4){

                    if(pressNum == matchNum){


                    } else {





          1 person found this helpful
          • 2. Re: Newbie actionscript question
            Rothrock Level 5

            A good way to do this might be using binary numbers.


            So imagine that a zero means the button is "off" and a 1 if the button is "on."


            So if you have 9 buttons and you give each button an id number that corresponds to 0 through 8. When coding it really helps to learn to start counting at zero! Also remember that just like base ten numbers binary numbers read from right to left. So if you want to have only button 1 (remember that is the second one because you have already learned to count starting at zero) then your code would be:




            or if number 8 and 3 are to be on your code would be:




            So you can do something like this:



            var myCode:String="100000100";

            var myCodeValue:Number=parseInt(myCode,2);

            var userCodeValue:Number=0;


            Then on each button you would have something like this:


            userCodeValue ^= 1<<this.ID;


            Remember ID is a property you have assigned each button to represent its "place" from zero to eight.


            What this code does is toggles the value of that place -- if it is zero when clicked it turns it to a one and vice versa.



            Finally whenever you want to check to see if the appropriate code has been found you would just compare the two values:



            trace("Found the right code.")

            } else {

            trace("Nope, try again.");



            The cool thing about this approach is that it is easy to expand to any length (well up to 15 or so) and it can be modified in a lot of ways.

            • 3. Re: Newbie actionscript question
              Rothrock Level 5

              D'oh! I mean that you can do this for up to 32 things.


              And I realized that I assumed you wanted the user to be able to turn them on and off multiple times until they got what they thought was right.


              Additionally there is no control for the order of turning them on. Ned's way is better if there is a requirement for ordering.