A
A
Alexander Vasilenko2015-08-23 10:00:51
Java
Alexander Vasilenko, 2015-08-23 10:00:51

How to make the keyboard work in a game written with libgdx?

Hello, friends.
Yesterday I decided to master libgdx, because I really want to pee games in my free time, in short, have some fun. But my mood was spoiled by a terrible thing.
I decided to start small and went through this lesson. The code of the whole program is at the very bottom of the page from this link.
Notice the render() method and these lines:

// обработка пользовательского ввода
        //обработка ввода мыши
        if(Gdx.input.isTouched()) {
            Vector3 touchPos = new Vector3();
            touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            camera.unproject(touchPos);
            bucket.x = touchPos.x - 64 / 2;
        }
        //обработка ввода с клавиатуры
        if(Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
        if(Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();

The problem is with the last two lines from this piece of code. The bottom line is that the program does not respond at all to any keystroke.
Are there any ideas? Maybe someone faced a problem?
OS: Mac OS X Yosemite;
IDE: IntelliJ IDEA

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iundersun, 2015-08-23
@SanchelliosProg

I used to work with InputProcessorom
it has suitable methods for handling both mouse/keyboard and touchscreen
here is an example:

public class GameScreen implements Screen, InputProcessor{

  //вызывается при создании нового экрана (Screen)
  public void show(){	
      //говорим что будем отбрабатывать нажатия тут
      Gdx.input.setInputProcessor(this); 
  }
  
  //работаем с клавиатурой
  public boolean keyDown(int key){
      if(key==Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
      if(key==Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();
  }
  
  //работаем с мышей/тачскрином
  public boolean touchDown(int screenX, int screenY, int pointer, int button){
      Vector3 touchPos = new Vector3(screenX, screenY);
            camera.unproject(touchPos);
            bucket.x = touchPos.x - 64 / 2;
  }
  
  public void render(float delta)
  {
    Gdx.gl.glClearColor(1.0F, 0.0F, 0.0F, 1.0F);
    Gdx.gl.glClear(16384);
    
    //рисуем что-то
  }
}

Similar questions
K
KhanTengri2011-06-17 23:56:41
Get from a remote web page?</a> <a class="content__item-button" href="/q/get-title-from-a-remote-web-page"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" class=""> <path fill-rule="evenodd" d="M15 14a1.002 1.002 0 01-1.703.71h-.004l-.003-.01-1.704-1.7H3c-1.104 0-2-.9-2-2V3c0-1.1.896-2 2-2h10c1.104 0 2 .9 2 2v11zM5 6c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1z"></path> </svg> <span class="dark">6</span><span class="dn">Reply</span> </a> </div> </div><div class="content__item"> <div class="content__item-img name-color-P"> <span>P</span></div> <div class="content__item-content"> <div class="content__item-row"> <a href="/q/how-to-solve-the-problem-permission-denied-when-starting-tomcat-from-the-command-line">P_Alexander</a><span>2018-02-17 02:18:21</span></div> <a class="content__item-title similar" href="/q/how-to-solve-the-problem-permission-denied-when-starting-tomcat-from-the-command-line">How to solve the problem (permission denied) when starting TomCat from the command line?</a> <a class="content__item-button" href="/q/how-to-solve-the-problem-permission-denied-when-starting-tomcat-from-the-command-line"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" class=""> <path fill-rule="evenodd" d="M15 14a1.002 1.002 0 01-1.703.71h-.004l-.003-.01-1.704-1.7H3c-1.104 0-2-.9-2-2V3c0-1.1.896-2 2-2h10c1.104 0 2 .9 2 2v11zM5 6c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1z"></path> </svg> <span class="dark">1</span><span class="dn">Reply</span> </a> </div> </div><div class="content__item"> <div class="content__item-img name-color-M"> <span>M</span></div> <div class="content__item-content"> <div class="content__item-row"> <a href="/q/how-to-gracefully-populate-static-data-when-unit-testing-in-java-junit">Matvey Slavenko</a><span>2015-10-14 21:08:51</span></div> <a class="content__item-title similar" href="/q/how-to-gracefully-populate-static-data-when-unit-testing-in-java-junit">How to gracefully populate static data when unit testing in Java, JUnit?</a> <a class="content__item-button" href="/q/how-to-gracefully-populate-static-data-when-unit-testing-in-java-junit"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" class=""> <path fill-rule="evenodd" d="M15 14a1.002 1.002 0 01-1.703.71h-.004l-.003-.01-1.704-1.7H3c-1.104 0-2-.9-2-2V3c0-1.1.896-2 2-2h10c1.104 0 2 .9 2 2v11zM5 6c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1z"></path> </svg> <span class="dark">0</span><span class="dn">Reply</span> </a> </div> </div><div class="content__item"> <div class="content__item-img name-color-A"> <span>A</span></div> <div class="content__item-content"> <div class="content__item-row"> <a href="/q/visualization-of-program-structure">andrwee</a><span>2015-10-15 16:06:38</span></div> <a class="content__item-title similar" href="/q/visualization-of-program-structure">Visualization of program structure?</a> <a class="content__item-button" href="/q/visualization-of-program-structure"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" class=""> <path fill-rule="evenodd" d="M15 14a1.002 1.002 0 01-1.703.71h-.004l-.003-.01-1.704-1.7H3c-1.104 0-2-.9-2-2V3c0-1.1.896-2 2-2h10c1.104 0 2 .9 2 2v11zM5 6c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1z"></path> </svg> <span class="dark">1</span><span class="dn">Reply</span> </a> </div> </div><div class="content__item"> <div class="content__item-img name-color-P"> <span>P</span></div> <div class="content__item-content"> <div class="content__item-row"> <a href="/q/how-to-implement-security-at-the-container-form-level">P_Alexander</a><span>2018-02-19 10:41:29</span></div> <a class="content__item-title similar" href="/q/how-to-implement-security-at-the-container-form-level">How to implement security at the container (form) level?</a> <a class="content__item-button" href="/q/how-to-implement-security-at-the-container-form-level"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" class=""> <path fill-rule="evenodd" d="M15 14a1.002 1.002 0 01-1.703.71h-.004l-.003-.01-1.704-1.7H3c-1.104 0-2-.9-2-2V3c0-1.1.896-2 2-2h10c1.104 0 2 .9 2 2v11zM5 6c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1z"></path> </svg> <span class="dark">0</span><span class="dn">Reply</span> </a> </div> </div> </div> </div> </div> <div class="content find"> <p class="find__p">Didn't find what you were looking for?</p><a class="find__btn" href="#!">Ask your question</a> </div> <div class="find sm"> <p class="find__title">Ask a Question </p> <form class="find__form" action="#!"> <input class="find__input" type="text" placeholder="Ask your question here"> <button class="find__btn" type="submit"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 16 16" class="nBqR1"><path fill-rule="evenodd" d="M12.696 7.256L9.657 4.217a1 1 0 00-1.415 1.414l1.344 1.344H3.949a1.001 1.001 0 000 2h5.637l-1.344 1.343a1.002 1.002 0 000 1.415c.391.39 1.024.39 1.415 0l3.039-3.04A.993.993 0 0013 7.975a.997.997 0 00-.293-.708l-.011-.011z"></path></svg></button> </form> <p class="find__span">731 491 924 answers to any question</p> </div> </div> <aside class="banners"> <p>banners </p> </aside> </div> </main> </div> <script src="/assets/theme/js/highlight/highlight.min.js"></script> <script src="/assets/theme/js/app.min.js?_v=20220404145216"> </script> <script>hljs.highlightAll();</script> </body> </html>