java - Libgdx ( Input) Keypress seems to skip something -


hello once again oh mighty people of internet! have decided go libgdx creating simple memory game become better @ java. have solved, , tested lot of things myself, have come across problem just. can't. figure. out.

i have created complete cluster of if statements create menu has buttons can selected arrow keys (i couldn't figure out how make them clickable buttons :p) , seems work fine except skips 1 of buttons high scores button. post levelselection screen class, since that's 1 i'm sure problem in.

the levelselection screen class:

package com.me.mygdxgame;  import com.badlogic.gdx.gdx; import com.badlogic.gdx.input; import com.badlogic.gdx.screen; import com.badlogic.gdx.graphics.gl10; import com.badlogic.gdx.graphics.texture; import com.badlogic.gdx.graphics.g2d.bitmapfont; import com.badlogic.gdx.graphics.g2d.spritebatch;   public class levelselection implements screen {            private spritebatch spritebatch;            private texture playb;            private texture exitb;            private texture hscoreb;            private texture background;            mygdxgame game;            private boolean playbutton;            private boolean quitbutton;            private boolean highscoresbutton;            bitmapfont  font;       public levelselection(mygdxgame game) {         this.game = game;       }        public void render(float delta) {         gdx.gl.glclearcolor( 0f, 0f, 0f, 1f );         gdx.gl.glclear(gl10.gl_color_buffer_bit);           spritebatch.begin();         spritebatch.draw(background, 0, 0);         spritebatch.draw(playb, 250, 450);         spritebatch.draw(exitb, 250, 350);         spritebatch.draw(hscoreb, 250, 400);         spritebatch.end();          //start navigation between menu buttons              if(playbutton == true && quitbutton == false && highscoresbutton == false && gdx.input.iskeypressed(input.keys.down)){                 playbutton = false;                 quitbutton = false;                 highscoresbutton = true;                 system.out.println("highscores button selected");              }             if(highscoresbutton == true && playbutton == false && quitbutton == false && gdx.input.iskeypressed(input.keys.down)){                highscoresbutton = false;                playbutton = false;                quitbutton = true;                system.out.println("quit button selected");      }             if(quitbutton == true && playbutton == false && highscoresbutton == false && gdx.input.iskeypressed(input.keys.up)){                 quitbutton = false;                 playbutton = false;                 highscoresbutton = true;                 system.out.println("highscores button selected");             }              if(highscoresbutton == true && quitbutton == false && playbutton == false && gdx.input.iskeypressed(input.keys.up)){                  quitbutton = false;                  highscoresbutton = false;                  playbutton = true;                  system.out.println("play button selected");              }               //end navigation between menu buttons                   if(playbutton == true && gdx.input.iskeypressed(input.keys.enter)){                      game.setscreen(game.gamescreen);                  }                  if(quitbutton == true && gdx.input.iskeypressed(input.keys.enter)){                      game.dispose();                  }                   //draw text according selected button                 if(highscoresbutton == true){                     spritebatch.begin();                     font.draw(spritebatch, "high score button selected!", 15, 15);                     spritebatch.end();                 }                 if(quitbutton == true){                     spritebatch.begin();                     font.draw(spritebatch, "quit button selected!", 15, 15);                     spritebatch.end();                 }                 if(playbutton == true){                     spritebatch.begin();                     font.draw(spritebatch, "play button selected!", 15, 15);                     spritebatch.end();                 }             } 

i believe problem game running @ high speeds, , reason when hit button (up or down) more once.

problem:

it runs @ approximately 60 fps. it'd difficult let happen once.


solution:

for 1 keystroke, allow 1 transition.

to achieve this,

  1. store last pressed key.
  2. if pressed key same stored key, nothing.
  3. update it's value after each button focus transition.
  4. set it's value null @ if key isn't pressed more (one more if condition :d )

notes:

  1. don't use boolean each button. makes conditions verbose. in stead, use integer int selectedbuttonindex = 0; , update it's value everytime.
  2. this not standard way implement menu in libgdx. trying implement scratch. framework provides many higher level functionalities when comes ui. try searching scene2d ui on internet. you'd find many resources. (you clickable buttons there. :d )

good luck.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -