3 Replies Latest reply on Feb 6, 2010 5:55 AM by Ned Murphy

    How do I go to next question/frame?(keyboard key AS3.0)

    xtachex

      Hi, I'm quite new in flash, and I'm trying to make a quiz.

      I would like to know what a.s. should I write to go to next question when I hit a keyboard key.

       

      Thanks,

      xtachex

        • 1. Re: How do I go to next question/frame?(keyboard key AS3.0)
          Ned Murphy Adobe Community Professional & MVP

          Without any idea of what your design approach is, that's hard to answer.  What code/element do you have in place for advancing to the next question at the present time?  If there is none, what action would need to be taken to get to the next question?

          • 2. Re: How do I go to next question/frame?(keyboard key AS3.0)
            xtachex Level 1

            until now I have the answers like this:

             

             

            //Question 6

             

             

            stop();

             

            bad6_btn.addEventListener(MouseEvent.CLICK,bad6Click)

             

            function bad6Click(event:MouseEvent):void

            {

            gotoAndStop("gameOver")

             

            }

             

             

             

             

            good6_btn.addEventListener(MouseEvent.CLICK,good6Click)

             

            function good6Click(event:MouseEvent):void

            {

            gotoAndStop("question7")

             

            }

             

             

            but now instead of going from Question 7 to Question 8 by clicking a button,  I would like to go to the next question by hitting a keyboard key (for ex "A")

            • 3. Re: How do I go to next question/frame?(keyboard key AS3.0)
              Ned Murphy Adobe Community Professional & MVP

              You can use something like the following at each frame where you would be changing to the next question:

               

              stage.addEventListener(KeyboardEvent.KEY_UP, nextQuestion);

               

              function nextQuestion(event:KeyboardEvent):void {
                    //trace(event.keyCode);
                    if(event.keyCode == Keyboard.RIGHT)
                    {
                          gotoAndStop("question7");
                          stage.removeEventListener(KeyboardEvent.KEY_UP, nextQuestion);
                    }

              }

               

              Another approach would be to just have that listener/function pair done such that it works for each advancement without having to rewrite it (same for your buttons).  This could be realized by creating a variable that is assigned the next question's frame label when you arrive in each question.

               

              var nextQuestion:String; // assigned at each question's labelled frame

               

              stage.addEventListener(KeyboardEvent.KEY_UP, nextQuestion);

               

              function nextQuestion(event:KeyboardEvent):void {
                    //trace(event.keyCode);
                    if(event.keyCode == Keyboard.RIGHT)
                    {
                          gotoAndStop(nextQuestion);
                  }

              }

               

              next_btn.addEventListener(MouseEvent.CLICK,nextClick)

               

              function nextClick(event:MouseEvent):void

              {

                   gotoAndStop(nextQuestion);

              }

               

              Lastly, I left the commented out line: //trace(event.keyCode);
              in the code in case you plan to use some other key.  Uncommenting that trace will help you more quickly determine which keyCode to specify.  It will produce a number, and that number would replace "Keyboard.RIGHT"

              1 person found this helpful