Skip to main content
Participating Frequently
January 10, 2019
Question

Need to load, use and unload a Keyboard Event Handler when Spacebar is pressed.

  • January 10, 2019
  • 1 reply
  • 564 views

Good evening everyone!

I am building a live, operator-based Jeopardy game and need to manually leave an initial Splash Screen on Frame 1 ("HOME") by pressing the Space Bar instead of  using a mouse click (Keyboard Event Handler not Mouse Event).  The problem seems to be that the destination frame ("RESET") already has another Keyboard Event Handler that uses the Space Bar for something different. All of my major Keyboard Event Handler code is on frame 10 ("RESET") except for the following, which is on the static Splash Screen on frame 1 *"HOME").


Essentially I am trying to make the Space Bar do double-duty by creating then unloading the Event Handler.  It's safe to assume it's not as easy as I hoped it would be...

The error messages refer to line 24, which is the line highlighted in red below:

MAIN, Layer 'Actions', Frame 1, Line 24, Column 101021: Duplicate function definition.
MAIN, Layer 'Actions', Frame 1, Line 24, Column 101023: Incompatible override.

... and here's the frame 1 code:

stage.addEventListener(KeyboardEvent.KEY_DOWN, startGame);

function startGame(event: KeyboardEvent.KEY_DOWN): void {

   if (event.keyCode == 32 ) {

       stage.removeEventListener(KeyboardEvent.KEY_DOWN,startGame);

       this.gotoAndStop("RESET");

  }

}

Thanks in advance!

This topic has been closed for replies.

1 reply

JoãoCésar17023019
Community Expert
Community Expert
January 10, 2019

Hi.

There are two main approaches you can use to handle this kind of situation.

1 - Add one single event listener in the beginning and then check for the current frame;

2 - Pass an anonymous function as the event handler and then inside of this function call another function declared as a variable that actually contains your code and that you can change it in any frame.

Examples:

APPROACH 1:

[Frame 1]

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

function keyDownHandler(e:KeyboardEvent):void

{

    if (e.keyCode == Keyboard.SPACE)

    {

          if (currentLabel == "home")

          {

              gotoAndStop("reset");

          }

          else if (currentLabel == "reset")

          {

              gotoAndStop("end");

          }

          else if (currentLabel == "end")

          {

              gotoAndStop("home");

          }

    }

}

stop();

if (!stage.hasEventListener(KeyboardEvent.KEY_DOWN))

     stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

APPROACH 2:

[Frame 1]

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

var onKeyDown:Function = function(e:KeyboardEvent):void

{

    if (e.keyCode == Keyboard.SPACE)

         gotoAndStop("reset");

};

stop();

if (!stage.hasEventListener(KeyboardEvent.KEY_DOWN))

     stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void{onKeyDown(e);});

[Frame 2]

onKeyDown = function(e:KeyboardEvent):void

{

    if (e.keyCode == Keyboard.SPACE)

         gotoAndStop("end");

};

[Frame 3]

onKeyDown = function(e:KeyboardEvent):void

{

    if (e.keyCode == Keyboard.SPACE)

         gotoAndStop("home");

};

I hope these all make sense.

Please don't hesitate to ask if you still have any further questions.

Regards,

JC

Participating Frequently
January 10, 2019

Thanks JC - I used the code from your first example, but altered it slightly as I do not need the spacebar to go anywhere but the RESET frame from the HOME frame.  I don't think it affected anything, and the space bar does indeed advance to RESET, but ONLY AFTER I click anywhere on the screen to "wake it up".  I had this problem in a previous post and was given the stage.focus = this; line of code.  Unfortunately placing that code in the frame 1 script didn't have any effect.

Here's what my frame 1 ("HOME") code looks like:

fscommand("fullscreen", "true");

stage.focus = this;

//-------------------------------------------------------------//

import flash.events.KeyboardEvent; 

import flash.ui.Keyboard; 

 

function keyDownHandler(e:KeyboardEvent):void { 

    if (e.keyCode == 32) { 

          if (currentLabel == "HOME")  {

               gotoAndStop("RESET"); 

          } 

   }

  }

 

if (!stage.hasEventListener(KeyboardEvent.KEY_DOWN)) 

     stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

stop();

Thanks again for your help!

JoãoCésar17023019
Community Expert
Community Expert
January 10, 2019

Excellent!

You're welcome!

My usage of the space bar was only an example. My goal was to show you the general idea.

I'm glad it helped you!