3 Replies Latest reply: Jun 15, 2013 6:44 AM by kglad RSS

    Key Press Problem

    Hydra19

      Hello!

      First i want to say, sorry my bad english.

       

      I'm new here,i searched on forums and web but i don't found the solution.

       

      I have 2 scenes:

      ''menu''

      ''level1''

       

      And i have a ''button'' in the ''level1''. This button is needed to start the game. It has only a code, that shows/hides/changes cursor. Nothing else.

       

      In ''menu'' i have 2 frames, the first is the ''mainmenu'', and the second is the ''levelselect'' ( where i have a button that redirects you to the scene ''level1'', it works.)

       

      I have this code in the scene ''level1'', but it only redirects me to ''menu'', frame 1,when i click that ''button''.

      But i don't know why, this thing works only when i click that button, but the 2 things are not related.

       

      This is the script for the ''escape'' key.

      ------------------------------------------------------------------------------------------ -----------------------

      stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

       

       

      function fl_KeyboardDownHandler(event:KeyboardEvent):void

      {

                  if(event.keyCode==27)

                {

                 gotoAndStop(1, "menu")        

                      }

                trace("escape" + event.altKey);

       

      }

      ------------------------------------------------------------------------------------------ ------------------------

       

       

      And this is script for the button:

      ( I have custom cursor that activates when i press the button, it works perfectly.

      Just i need to click the button and the escape to redirect to the first scene. Dont know why.)

       

      ------------------------------------------------------------------------------------------ -----------------------

      import flash.events.MouseEvent;

       

      stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

      function fl_KeyboardDownHandler(event:KeyboardEvent):void

      {

                  if(event.keyCode==27)

                {

                 gotoAndStop(1, "menu")                                                                                           //this is the code, when i press the escape key

                 dot.removeEventListener(Event.ENTER_FRAME, showcustomcursor);

                 stage.removeChild(dot);

                 Mouse.show();

       

                      }

                trace("Escape is " + event.altKey);                                                                                    //this line is only for test, so i know i pressed the ''escape''

       

      }

       

       

      startbutton.addEventListener(MouseEvent.CLICK, activate);                                            //personal test too, i can see the whole stuff is active

      function activate(event:MouseEvent):void

      {

                trace("Activated");

      }

       

      startbutton.addEventListener(MouseEvent.CLICK, showcustomcursor)

      function showcustomcursor(event:MouseEvent):void

      {

        stage.addChild(dot);

        dot.mouseEnabled = false;

        dot.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);

       

       

        function fl_CustomMouseCursor(event:Event)

        {

                dot.x = stage.mouseX;

                dot.y = stage.mouseY;

        }

        Mouse.hide();                                                             //hides the cursor

      }

      ------------------------------------------------------------------------------------------ ----------------

       

       

      Thank you, and sorry.

       

       

      EDIT: I want to press only escape key to quit to main menu, not button+escape.

        • 1. Re: Key Press Problem
          kglad ACP/MVP

          never nest named functions. once you unnest them, copy and past your unedited code because you showed two different keylistener functions with the same name.

           

          function showcustomcursor(event:MouseEvent):void

          {

            stage.addChild(dot);

            dot.mouseEnabled = false;

            dot.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);

          Mouse.hide();

          }

           

            function fl_CustomMouseCursor(event:Event)

            {

                    dot.x = stage.mouseX;

                    dot.y = stage.mouseY;

            }

          • 2. Re: Key Press Problem
            Hydra19 Community Member

            Thank you, this worked!

            I have now only a really very little problem, but this is not important ( or this is not problem, this is normal?):

            I have to click the screen + escape to quit to the main menu.

             

            Select level *click* ----> Level 1 *click* ----> *press escape*                does't works.

            Select level *click* ----> Level 1 *click* ----> *click anywhere* *press escape*        everything works.

             

            This happens because i switched to a new scene?

            • 3. Re: Key Press Problem
              kglad ACP/MVP

              no, that's not normal behavior.