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

时间:2011-04-29 14:01:43

标签: java sprite 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() {
}

}

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

4 个答案:

答案 0 :(得分:31)

对于您手头的任务,您甚至可能不需要实现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://www.badlogicgames.com/forum有论坛,你也可以在这里询问libgdx的具体问题!

HTH, 马里奥

答案 1 :(得分:2)

在你的游戏循环方法中(可能渲染或制作一个)需要添加一个输入处理程序(实现 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 *方法并检查是否按下某个键并从马里奥增加或减少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

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

希望帮助

答案 2 :(得分:2)

您可以使用界面KeyListener来检测键盘操作。

public class Game implements ApplicationListener, KeyListener {

@Override
public void create() {
    //Important
    this.addKeyListener(this);

    // 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
        }
    }

}

答案 3 :(得分:1)

我首选的方法是使用一个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 a.k.a 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 a.k.a 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 a.k.a 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 a.k.a 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 a.k.a 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 a.k.a 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 a.k.a 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 a.k.a 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;
}