繁体   English   中英

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

[英]How to move a sprite with the keyboard keys using 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();
}

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

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

hth,马里奥

在您的游戏循环方法(可能是渲染或制作)中需要添加一个输入处理程序(实现InputProcessor )。 class 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);

和 class Input.Keys有很多 static 变量作为键码。 例如:

        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。

EDIT: Check this links: 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

希望帮助

您可以使用接口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
        }
    }

}

我的首选方法是使用 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);

最后,如果我想检查是否按下了某个键,我可以 go

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM