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

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

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值?

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

 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(); } 

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

顺便说一句,我们在http://sofzh.miximages.com/java/ TODO Auto-generated method stub 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 keyPressed(KeyEvent e) { if (e.getKeyCode() == 68) { //it’s the ‘D’ key //Move your mario } } }

在你的游戏循环方法(可能渲染或制作一个)中需要添加一个输入处理程序(实现InputProcessor )。 InputProcessor类有以下方法:

 public boolean keyDown (int keycode); /** * Called when a key was released * * @param keycode one of the constants in {@link Input.Keys} * @return whether the input was processed */ public boolean keyUp (int keycode); /** * Called when a key was typed * * @param character The character * @return whether the input was processed */ public boolean keyTyped (char character); 

Input.Keys类有很多静态变量作为键码。 例如:

  public static final int D = 32; public static final int A = 29; public static final int S = 47; public static final int W = 51; 

因此,实现key *方法并检查是否按下某个键并从mario增加或减少X / Y.

编辑:检查以下链接: http : //code.google.com/p/libgdx/source/browse/trunk/gdx/src/com/badlogic/gdx/InputProcessor.java http://code.google.com/p /libgdx/source/browse/trunk/gdx/src/com/badlogic/gdx/Input.java

或者,在主干中,如何处理演示输入: http : //code.google.com/p/libgdx/source/browse/#svn%2Ftrunk%2Fdemos

希望有所帮助

我首选的方法是使用一个InputController,它存储所有准备好进行检查的标准密钥。

 import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.math.Vector2; public class KeyboardController implements InputProcessor { public boolean left,right,up,down; public boolean isMouse1Down, isMouse2Down,isMouse3Down; public boolean isDragged; public Vector2 mouseLocation = new Vector2(0,0); @Override public boolean keyDown(int keycode) { boolean keyProcessed = false; switch (keycode) // switch code base on the variable keycode { case Keys.LEFT: // if keycode is the same as Keys.LEFT aka 21 left = true; // do this keyProcessed = true; // we have reacted to a keypress break; case Keys.RIGHT: // if keycode is the same as Keys.LEFT aka 22 right = true; // do this keyProcessed = true; // we have reacted to a keypress break; case Keys.UP: // if keycode is the same as Keys.LEFT aka 19 up = true; // do this keyProcessed = true; // we have reacted to a keypress break; case Keys.DOWN: // if keycode is the same as Keys.LEFT aka 20 down = true; // do this keyProcessed = true; // we have reacted to a keypress } return keyProcessed; // return our peyProcessed flag } @Override public boolean keyUp(int keycode) { boolean keyProcessed = false; switch (keycode) // switch code base on the variable keycode { case Keys.LEFT: // if keycode is the same as Keys.LEFT aka 21 left = false; // do this keyProcessed = true; // we have reacted to a keypress break; case Keys.RIGHT: // if keycode is the same as Keys.LEFT aka 22 right = false; // do this keyProcessed = true; // we have reacted to a keypress break; case Keys.UP: // if keycode is the same as Keys.LEFT aka 19 up = false; // do this keyProcessed = true; // we have reacted to a keypress break; case Keys.DOWN: // if keycode is the same as Keys.LEFT aka 20 down = false; // do this keyProcessed = true; // we have reacted to a keypress } return keyProcessed; // return our peyProcessed flag } @Override public boolean keyTyped(char character) { return false; } @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { if(button == 0){ isMouse1Down = true; }else if(button == 1){ isMouse2Down = true; }else if(button == 2){ isMouse3Down = true; } mouseLocation.x = screenX; mouseLocation.y = screenY; return false; } @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { isDragged = false; //System.out.println(button); if(button == 0){ isMouse1Down = false; }else if(button == 1){ isMouse2Down = false; }else if(button == 2){ isMouse3Down = false; } mouseLocation.x = screenX; mouseLocation.y = screenY; return false; } @Override public boolean touchDragged(int screenX, int screenY, int pointer) { isDragged = true; mouseLocation.x = screenX; mouseLocation.y = screenY; return false; } @Override public boolean mouseMoved(int screenX, int screenY) { mouseLocation.x = screenX; mouseLocation.y = screenY; return false; } @Override public boolean scrolled(int amount) { return false; } } 

然后,我需要做的就是在游戏的create方法中使用KeyboardController

 controller = new KeyboardController(); 

然后告诉GDX使用它来监听事件

 Gdx.input.setInputProcessor(controller); 

最后,如果我想检查按键是否按下,我可以去

 if(controller.left){ player.x -= 1; }