Libgdx(输入)Keypress似乎跳过了一些东西

时间:2014-02-06 02:53:23

标签: java input keyboard libgdx

你好再次哦哦强大的互联网人!我决定使用libGDX创建一个简单的内存游戏,以便在java中变得更好。我已经解决了,并且已经自己测试了很多东西,但是我遇到了一个问题,我只是。不能。数字。进行。

我创建了一个完整的if语句集群来创建一个菜单,其中包含可以通过箭头键选择的按钮(我无法弄清楚如何使它们成为可点击的按钮:P)它似乎工作正常它会跳过其中一个按钮,这是我的高分按钮。我只会发布levelSelection Screen类,因为那是我确定问题所在的那个。

levelSelection Screen类:

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 is 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 is 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 is 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 is 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 to selected button
                if(highScoresButton == true){
                    spriteBatch.begin();
                    font.draw(spriteBatch, "High Score Button is Selected!", 15, 15);
                    spriteBatch.end();
                }
                if(quitButton == true){
                    spriteBatch.begin();
                    font.draw(spriteBatch, "Quit Button is Selected!", 15, 15);
                    spriteBatch.end();
                }
                if(playButton == true){
                    spriteBatch.begin();
                    font.draw(spriteBatch, "Play Button is Selected!", 15, 15);
                    spriteBatch.end();
                }
            }

我认为问题在于游戏是以高速运行的,出于某种原因,当我点击按钮(向上或向下)时,它不止一次。

1 个答案:

答案 0 :(得分:3)

问题:

通常以大约60 fps的速度运行。所以很明显很难让它只发生一次。


解决方案:

对于一次击键,您只能进行一次击球。

要实现这一目标,

  1. 存储最后按下的键。
  2. 如果当前按下的键与存储的键相同,则不执行任何操作。
  3. 在每次按钮焦点转换后更新它的值。
  4. 如果不再按下该键,则将其值设置为null(如果条件:D则再按一次)

  5. 备注:

    1. 不要为每个按钮使用布尔值。它使条件非常冗长。相反,使用像int selectedButtonIndex = 0;这样的整数,并且每次都更新它的值。
    2. 这不是在libgdx中实现菜单的标准方法。你正试图从头开始实现一切。但是当涉及到ui时,框架已经提供了许多更高级别的功能。尝试在互联网上搜索 scene2d ui 。你会找到很多资源。 (你会在那里得到可点击的按钮。:D)
    3. 祝你好运。