Skip navigation
Hydra19
Currently Being Moderated

Key Press Problem

Jun 15, 2013 3:45 AM

Tags: #problem #error #text #actionscript #key #fix #escape #stuff #keypress #escape_key

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.

 
Replies
  • kglad
    72,220 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 15, 2013 5:35 AM   in reply to Hydra19

    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;

      }

     
    |
    Mark as:
  • kglad
    72,220 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 15, 2013 6:44 AM   in reply to Hydra19

    no, that's not normal behavior.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points