4 Replies Latest reply on Mar 8, 2011 11:39 AM by woodenroots

    Need help AS3 question

    woodenroots

      I am programming a music game where there are five movieclips (acting as buttons) representing five notes of a piano.  When the user clicks on each one, they play a note.  I would like to know how I can program it so that if the user clicks a specific five note sequence, it will trigger an animated movieclip in the timeline.

       

      Example:

      Note_1 + Note_4 + Note_2 + Note_5 + Note_3 = Animation_movie

       

      I am using Flash CS4 and Actionscript 3.  I've searched everywhere for a posting on this and couldn't find it anywhere.  Any help would be greatly appreciated!

        • 1. Re: Need help AS3 question
          Ned Murphy Adobe Community Professional & MVP

          You aren't likely to find anything in the way of a tutorial dedicated to this specific design.  There are numerous ways you could do this but they all require creating some form of memory to keep track of what mc's were clicked.  One way would be to create a String variable that you append characters to and then compare after a number of keys have been pressed.

           

          var correctSequence:String = "14253";

           

          var clickedSequence:String = "";

           

          and as you click each mc you add its number to the clickedSequence string and compare it to the correctSequence string.   If/when they match, you trigger the animation

          1 person found this helpful
          • 2. Re: Need help AS3 question
            woodenroots Level 1

            Thanks Ned, I'll try it and do some experimenting.

            • 3. Re: Need help AS3 question
              woodenroots Level 1

              I tried using the array command, I feel like I'm getting closer, but I still can't get it to work.  This is what I have so far

              ActionScript Code:
              var noteArray:Array = new Array(note05_mc, note01_mc, note03_mc, note04_mc, note02_mc);
              var buttonArray:Array = new Array();

              stage.addEventListener(MouseEvent.CLICK,musicsequence);
              function musicsequence(event:MouseEvent):void {
                  if (noteArray.toString().indexOf(event.toString()) != -1) {
                      buttonArray.push(event);
                      if (buttonArray.toString().indexOf(noteArray.toString()) != -1) {
                             
                          // (This is where my event would go)
              };

                      trace("Hey, it works!");
                      buttonArray = new Array();

              } else {       
                      buttonArray = new Array();
                  };
              };
              • 4. Re: Need help AS3 question
                woodenroots Level 1

                Actually, I got it, someone on another forum figured it out!

                 

                var notes:Array = [note01_mc, note02_mc, note03_mc, note04_mc, note05_mc];
                var sequence:Array = [4,0,2,3,1]; // change this to change sequence...
                var correct:int;

                addEventListener(MouseEvent.CLICK,onClick);

                function onClick(e:MouseEvent):void {
                    if (notes.indexOf(e.target) == sequence[correct]) {
                        if (++correct == sequence.length) {
                            doSomeWinning();
                            correct = 0;
                        }
                    }
                    else {
                        trace("Failing! start again...");
                        correct = 0;
                    }
                }

                function doSomeWinning():void {
                    trace("Winning!");
                }