Highlighted

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

Community Beginner ,
Jan 09, 2019

Copy link to clipboard

Copied

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!

TOPICS
ActionScript

Views

163

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

Community Beginner ,
Jan 09, 2019

Copy link to clipboard

Copied

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!

TOPICS
ActionScript

Views

164

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 09, 2019 0
Adobe Community Professional ,
Jan 10, 2019

Copy link to clipboard

Copied

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

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 10, 2019 0
Community Beginner ,
Jan 10, 2019

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 10, 2019 1
Adobe Community Professional ,
Jan 10, 2019

Copy link to clipboard

Copied

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!

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 10, 2019 0
Community Beginner ,
Jan 10, 2019

Copy link to clipboard

Copied

I'm not quite out of the woods yet.  Any idea why I first have to click on the screen to allow the spacebar trigger the action?  If I play the game and then return to the HOME frame it works perfectly, but when I start the game the first time I have to click on the screen before the space bar is recognized.  I added the stage.focus=this(); code to no avail. Any ideas?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 10, 2019 0
Adobe Community Professional ,
Jan 10, 2019

Copy link to clipboard

Copied

When testing from inside of Animate CC, keyboard events may not work the way we expect.

But if you test from outside of Animate CC, by running the SWF from the OS file explorer, you'll see that you don't need to click the screen first.

Please tell me if it works for you.

Regards,

JC

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 10, 2019 0
Adobe Community Professional ,
Jan 10, 2019

Copy link to clipboard

Copied

The code should be: stage.focus = this;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 10, 2019 0
Community Beginner ,
Jan 10, 2019

Copy link to clipboard

Copied

When you're right you're right - it works perfectly when published as a Mac Projector App!  Thanks again!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 10, 2019 1
Adobe Community Professional ,
Jan 10, 2019

Copy link to clipboard

Copied

Wonderful!

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 10, 2019 0