1. <i id='F6AHE'><tr id='F6AHE'><dt id='F6AHE'><q id='F6AHE'><span id='F6AHE'><b id='F6AHE'><form id='F6AHE'><ins id='F6AHE'></ins><ul id='F6AHE'></ul><sub id='F6AHE'></sub></form><legend id='F6AHE'></legend><bdo id='F6AHE'><pre id='F6AHE'><center id='F6AHE'></center></pre></bdo></b><th id='F6AHE'></th></span></q></dt></tr></i><div id='F6AHE'><tfoot id='F6AHE'></tfoot><dl id='F6AHE'><fieldset id='F6AHE'></fieldset></dl></div>

        <bdo id='F6AHE'></bdo><ul id='F6AHE'></ul>

      <small id='F6AHE'></small><noframes id='F6AHE'>

        <tfoot id='F6AHE'></tfoot><legend id='F6AHE'><style id='F6AHE'><dir id='F6AHE'><q id='F6AHE'></q></dir></style></legend>

        如何使用 libGDX 使用键盘键移动精灵?

        时间:2023-05-20
          <i id='HptII'><tr id='HptII'><dt id='HptII'><q id='HptII'><span id='HptII'><b id='HptII'><form id='HptII'><ins id='HptII'></ins><ul id='HptII'></ul><sub id='HptII'></sub></form><legend id='HptII'></legend><bdo id='HptII'><pre id='HptII'><center id='HptII'></center></pre></bdo></b><th id='HptII'></th></span></q></dt></tr></i><div id='HptII'><tfoot id='HptII'></tfoot><dl id='HptII'><fieldset id='HptII'></fieldset></dl></div>
            <bdo id='HptII'></bdo><ul id='HptII'></ul>
              <tbody id='HptII'></tbody>

            <small id='HptII'></small><noframes id='HptII'>

            • <legend id='HptII'><style id='HptII'><dir id='HptII'><q id='HptII'></q></dir></style></legend>
              • <tfoot id='HptII'></tfoot>
                  本文介绍了如何使用 libGDX 使用键盘键移动精灵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我刚刚开始使用 java 和 libgdx 并拥有此代码,非常简单,它将精灵打印到屏幕上.这很好用,我从中学到了很多.

                  i have just started using java and libgdx and have this code, very simply it prints a sprite onto the screen. This works perfectly, and i learnt a lot from it.

                  package com.MarioGame;
                  
                  import com.badlogic.gdx.ApplicationListener;
                  import com.badlogic.gdx.Gdx;
                  import com.badlogic.gdx.files.FileHandle;
                  import com.badlogic.gdx.graphics.GL10;
                  import com.badlogic.gdx.graphics.Texture;
                  import com.badlogic.gdx.graphics.g2d.Sprite;
                  import com.badlogic.gdx.graphics.g2d.SpriteBatch;
                  import com.badlogic.gdx.graphics.g2d.TextureRegion;
                  import com.badlogic.gdx.InputProcessor;
                  
                  public class Game implements ApplicationListener {
                  private SpriteBatch batch;
                  private Texture marioTexture;
                  private Sprite mario;
                  private int marioX;
                  private int marioY;
                  
                  @Override
                  public void create() {
                      batch = new SpriteBatch();
                      FileHandle marioFileHandle = Gdx.files.internal("mario.png"); 
                      marioTexture = new Texture(marioFileHandle);
                      mario = new Sprite(marioTexture, 0, 158, 32, 64);
                      marioX = 0;
                      marioY = 0;
                  }
                  
                  @Override
                  public void render() {
                      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
                      batch.begin();
                      batch.draw(mario, marioX, marioY);
                      batch.end();
                  }
                  
                  
                  @Override
                  public void resume() {
                  }
                  
                  @Override
                  public void resize(int width, int height) {
                  }
                  
                  @Override
                  public void pause() {
                  }
                  
                  @Override
                  public void dispose() {
                  }
                  

                  }

                  当用户在键盘上按下 D 时,我将如何修改 marioX 值?

                  How would I modify the marioX value when a user presses D on their keyboard?

                  推荐答案

                  对于您手头的任务,您甚至可能不需要实现 InputProcessor.你可以像这样在你的 render() 方法中使用 Input.isKeyPressed() 方法.

                  For your task at hand you might not even need to implement InputProcessor. You can use the Input.isKeyPressed() method in your render() method like this.

                  float marioSpeed = 10.0f; // 10 pixels per second.
                  float marioX;
                  float marioY;
                  
                  public void render() {
                     if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) 
                        marioX -= Gdx.graphics.getDeltaTime() * marioSpeed;
                     if(Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) 
                        marioX += Gdx.graphics.getDeltaTime() * marioSpeed;
                     if(Gdx.input.isKeyPressed(Keys.DPAD_UP)) 
                        marioY += Gdx.graphics.getDeltaTime() * marioSpeed;
                     if(Gdx.input.isKeyPressed(Keys.DPAD_DOWN)) 
                        marioY -= Gdx.graphics.getDeltaTime() * marioSpeed;
                  
                     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
                     batch.begin();
                     batch.draw(mario, (int)marioX, (int)marioY);
                     batch.end();
                  }
                  

                  还请注意,我制作了 mario 浮动的位置坐标并将运动更改为基于时间的运动.marioSpeed 是 mario 每秒应沿任意方向行进的像素数.Gdx.graphics.getDeltaTime() 以秒为单位返回自上次调用 render() 以来经过的时间.在大多数情况下,实际上不需要强制转换为 int.

                  Also note that i made the position coordinates of mario floats and changed the movement to time-based movement. marioSpeed is the number of pixels mario should travel in any direction per second. Gdx.graphics.getDeltaTime() returns you the time passed since the last call to render() in seconds. The cast to int is actually not necessary in most situations.

                  顺便说一句,我们在 http://www.badlogicgames.com/forum 设有论坛,您可以在其中提问libgdx 的具体问题!

                  Btw, we have forums at http://www.badlogicgames.com/forum where you ask libgdx specific questions as well!

                  hth,马里奥

                  这篇关于如何使用 libGDX 使用键盘键移动精灵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 libgdx 的类的单元测试 下一篇:如何在 Libgdx 中调整精灵的大小?

                  相关文章

                  最新文章

                1. <tfoot id='1uuX1'></tfoot>
                      <bdo id='1uuX1'></bdo><ul id='1uuX1'></ul>

                      <i id='1uuX1'><tr id='1uuX1'><dt id='1uuX1'><q id='1uuX1'><span id='1uuX1'><b id='1uuX1'><form id='1uuX1'><ins id='1uuX1'></ins><ul id='1uuX1'></ul><sub id='1uuX1'></sub></form><legend id='1uuX1'></legend><bdo id='1uuX1'><pre id='1uuX1'><center id='1uuX1'></center></pre></bdo></b><th id='1uuX1'></th></span></q></dt></tr></i><div id='1uuX1'><tfoot id='1uuX1'></tfoot><dl id='1uuX1'><fieldset id='1uuX1'></fieldset></dl></div>
                    1. <legend id='1uuX1'><style id='1uuX1'><dir id='1uuX1'><q id='1uuX1'></q></dir></style></legend>

                      <small id='1uuX1'></small><noframes id='1uuX1'>