Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Key Press Listener not working properly?

Community Beginner ,
Jan 08, 2019 Jan 08, 2019

I'll try to keep this simple, but I am experiencing what seems like a glitch as I cannot figure out why it isn't working.  I have built a Jeopardy game intended to be played by a live audience but is controlled from backstage. Everything is driven by Key Press Events except for the actual "Answer" selection which is a mouse click on the Game Board. Everything is working as intended except for 1 particular scenario:  On the first Frame of the timeline I have the 6x5 Jeopardy Game Board Movie Clip automatically build screen-by-screen (to the beep boop sound effect).  There are also a Scoreboard MC and a Categories MC placed on the stage. When the Game Board is completely loaded, I then need the 6 Categories along the top to be revealed on cue as the Host on stage reads them off.

Here is the bug (that's what I'm calling it): The Categories Movie Clip is already on the stage, waiting for a cue from the Space Bar to advance through the Categories.  When I press the Space  Bar, I get an error sound (as if the Space Bar has no listener) and the Categories Movie Clip does not respond.  However, if I use the mouse to click anywhere on the screen just once, the Space Bar is then "active" and I can cue through the categories as intended.  Note that there are no buttons on the stage except for the Game Board itself (the "Answers" are all buttons) but nothing else.  I can't figure out why I am not able to simply cue the build, and how the mouse click on the stage does anything when there is no clickable object?

Here's the code from the first frame: I have highlighted the Space Bar code in red. I am sure I've done everything in long-form and that there are probably shorter or easier ways to do everything but I am not a Guru!

fscommand("fullscreen", "true");

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

// Reset all Movie Clips //

// Sets Game Board MC to Visible and to play from frame 1 //


this.gameBoard.visible = true;

this.gameBoard.gotoAndPlay(1);

// Sets Scoreboard MC to frame 1 //

this.scores.gotoAndStop(1);

// Sets Categories MC to frame 1 //

this.categories.gotoAndStop(1);

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

// Declare and Show Variables //

// Sets Player 1 Score to 0 //

var player1score: Number = 0;

trace(player1score);

this.scores.player1_MC.score.text = String(player1score);

// Sets Player 2 Score to 0 //

var player2score: Number = 0;

trace(player2score);

this.scores.player2_MC.score.text = String(player2score);

// Sets Player 3 Score to 0 //

var player3score: Number = 0;

trace(player3score);

this.scores.player3_MC.score.text = String(player3score);

// Resets Game Board Answers Tally to 30 //

var gameBoardTally: Number = 30;

trace(gameBoardTally);

this.progress.text = String(gameBoardTally);

// Reets Current Player to 0 //

var currentPlayer: Number = 0;

trace(currentPlayer);

this.activePlayer.text = String(currentPlayer);

// Sets initial value of Selected Answer to 0 //

var answerValue: Number = 0;

trace(answerValue);

this.value.text = String(answerValue);

// Shows current frame on main timeline //

var whereAmI: Number = this.currentFrame;

trace("I am on Frame " + whereAmI);

this.frameNumber.text = String(whereAmI);

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

//Sets All Player's Score windows to unhighlighted first frame//

this.scores.player1_MC.gotoAndStop(1);

this.scores.player2_MC.gotoAndStop(1);

this.scores.player3_MC.gotoAndStop(1);

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

// All Keypress Events Listener //

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

function fl_KeyboardDownHandler(event: KeyboardEvent): void {

  trace("Key Code Pressed: " + event.keyCode);

  // B KEY - Go Back to Game Board in Progress //

  if (event.keyCode == 66) {

  if (gameBoardTally < 1) {

  gotoAndStop("END");

  } else {

  gotoAndStop("GAME");

  }

  }

  // SPACEBAR - Trigger Category Label Builds //

  if (event.keyCode == 32) {

  this.categories.play();

  }

  // R KEY - Reset Game in Progress (Restart) //

  if (event.keyCode == 82) {

  this.gotoAndStop(1);


  // Resets all 30 screens/buttons to visible  //

  this.gameBoard.A100.visible = true;

  this.gameBoard.A200.visible = true;

  this.gameBoard.A300.visible = true;

  this.gameBoard.A400.visible = true;

  this.gameBoard.A500.visible = true;

  this.gameBoard.B100.visible = true;

  this.gameBoard.B200.visible = true;

  this.gameBoard.B300.visible = true;

  this.gameBoard.B400.visible = true;

  this.gameBoard.B500.visible = true;

  this.gameBoard.C100.visible = true;

  this.gameBoard.C200.visible = true;

  this.gameBoard.C300.visible = true;

  this.gameBoard.C400.visible = true;

  this.gameBoard.C500.visible = true;

  this.gameBoard.D100.visible = true;

  this.gameBoard.D200.visible = true;

  this.gameBoard.D300.visible = true;

  this.gameBoard.D400.visible = true;

  this.gameBoard.D500.visible = true;

  this.gameBoard.E100.visible = true;

  this.gameBoard.E200.visible = true;

  this.gameBoard.E300.visible = true;

  this.gameBoard.E400.visible = true;

  this.gameBoard.E500.visible = true;

  this.gameBoard.F100.visible = true;

  this.gameBoard.F200.visible = true;

  this.gameBoard.F300.visible = true;

  this.gameBoard.F400.visible = true;

  this.gameBoard.F500.visible = true;

  }

  // E KEY - Ends Game - Goes to Final Totals/Declares Winner or Winners (ties) //

  if (event.keyCode == 69) {

  gotoAndStop("END");

  // Determines which player has the high score //

  if (player1score > player2score) {

  if (player1score > player3score) {

  this.scores.player1_MC.gotoAndStop(4);

  }

  }

  if (player2score > player1score) {

  if (player2score > player3score) {

  this.scores.player2_MC.gotoAndStop(4);

  }

  }

  if (player3score > player1score) {

  if (player3score > player2score) {

  this.scores.player3_MC.gotoAndStop(4);

  }

  }

  // Determines if there is a 2-way tie //

  if (player1score == player2score) {

  if (player1score > player3score) {

  this.scores.player1_MC.gotoAndStop(4);

  this.scores.player2_MC.gotoAndStop(4);

  }

  }

  if (player2score == player3score) {

  if (player2score > player1score) {

  this.scores.player2_MC.gotoAndStop(4);

  this.scores.player3_MC.gotoAndStop(4);

  }

  }

  if (player3score == player1score) {

  if (player3score > player2score) {

  this.scores.player1_MC.gotoAndStop(4);

  this.scores.player3_MC.gotoAndStop(4);

  }

  }

  // Determines if there is a 3-way tie //

  if (player1score == player2score) {

  if (player1score == player3score) {

  this.scores.player1_MC.gotoAndStop(4);

  this.scores.player2_MC.gotoAndStop(4);

  this.scores.player3_MC.gotoAndStop(4);

  }

  }

  }

  // 1 KEY - Player One "Rings In" //

  if (event.keyCode == 49) {

  currentPlayer = 1;

  trace(currentPlayer);

  this.activePlayer.text = String(currentPlayer);

  this.scores.player1_MC.gotoAndStop(2);

  }

  // 2 KEY - Player Two "Rings In" //

  if (event.keyCode == 50) {

  currentPlayer = 2;

  trace(currentPlayer);

  this.activePlayer.text = String(currentPlayer);

  this.scores.player2_MC.gotoAndStop(2);

  }

  // 3 KEY - Player Three "Rings In" //

  if (event.keyCode == 51) {

  currentPlayer = 3;

  trace(currentPlayer);

  this.activePlayer.text = String(currentPlayer);

  this.scores.player3_MC.gotoAndStop(2);

  }

  //Minus Key//

  if (event.keyCode == 189) {

  this.audio_FX.gotoAndPlay("WrongAnswer");

  if (currentPlayer == 1) {

  player1score = player1score - answerValue;

  this.scores.player1_MC.score.text = String(player1score);

  this.scores.player1_MC.gotoAndStop(3);

  }

  if (currentPlayer == 2) {

  player2score = player2score - answerValue;

  this.scores.player2_MC.score.text = String(player2score);

  this.scores.player2_MC.gotoAndStop(3);

  }

  if (currentPlayer == 3) {

  player3score = player3score - answerValue;

  this.scores.player3_MC.score.text = String(player3score);

  this.scores.player3_MC.gotoAndStop(3);

  }

  }

  //Plus Key//

  if (event.keyCode == 187) {

  this.audio_FX.gotoAndPlay("RightAnswer");

  if (currentPlayer == 1) {

  player1score = player1score + answerValue;

  this.scores.player1_MC.score.text = String(player1score);

  gotoAndStop("GAME");

  }

  if (currentPlayer == 2) {

  player2score = player2score + answerValue;

  this.scores.player2_MC.score.text = String(player2score);

  gotoAndStop("GAME");

  }

  if (currentPlayer == 3) {

  player3score = player3score + answerValue;

  this.scores.player3_MC.score.text = String(player3score);

  gotoAndStop("GAME");

  }

  }

}

TOPICS
ActionScript
522
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 08, 2019 Jan 08, 2019

A much simpler test would be to make a new FLA that only has this script:

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

function fl_KeyboardDownHandler(event: KeyboardEvent): void {

  trace(event);

}

Do a Test Movie, and you'll see that there is nothing traced until after you click once on the blank stage of the test window. After that the trace happens ok.

I think this is a security related limitation. Without requiring a click of some sort it would be possible to have a SWF hidden on a web page, that just sits there reading every key that the user presses.

There are at least two easy solutions. One is to have some sort of Start button that has to be clicked before it goes to the screen where the keyboard is expected to work. The other is to make it be an AIR for Desktop FLA. All of your ActionScript would still work, but because it ends up being run as an application, and not using a browser plugin, the security rules are different. The first key press will be read ok.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 08, 2019 Jan 08, 2019

I added a Splash Screen which requires a Mouse Click to proceed to what was the first frame but the problem still remains.  Not familiar with AIR but will give it a shot.  This game will never be launched anywhere online but will always run from a computer in a live setting.

Thanks for the reply!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 08, 2019 Jan 08, 2019

If you can put the FLA online somewhere I could give it a try, to see if I can tell what is going wrong.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 08, 2019 Jan 08, 2019

Just sent you a PM with a link.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 08, 2019 Jan 08, 2019

For anyone following along, adding this solves the issue:

stage.focus = this;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 08, 2019 Jan 08, 2019
LATEST

THANK YOU!!!  I was hoping it was something simple and was thinking it might be a focus issue but had no idea what to do about it.

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