Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用libGDX使用键盘键移动精灵?_Java_Sprite_Libgdx - Fatal编程技术网

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

Java 如何使用libGDX使用键盘键移动精灵?,java,sprite,libgdx,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.b

我刚刚开始使用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)。 类
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
有许多静态变量作为keycodes。 例如:

        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/Input.java

或者,在trunk中,演示如何处理输入:


希望获得帮助

您可以使用界面
键盘侦听器
检测键盘操作

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

}

对于手头的任务,您甚至可能不需要实现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();
}
还请注意,我制作了mario浮动的位置坐标,并将移动更改为基于时间的移动。marioSpeed是mario每秒在任何方向上移动的像素数。getDeltaTime()返回自上次调用render()以来经过的时间(以秒为单位)。在大多数情况下,转换为int实际上是不必要的

顺便说一句,我们有论坛,在那里你问libgdx的具体问题以及

嗯,,
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;
    }
}
然后我需要做的就是在你的游戏的创建方法中使用键盘控制器

controller = new KeyboardController();
然后告诉GDX使用它来监听事件

Gdx.input.setInputProcessor(controller);
最后,如果我想检查是否按下了一个键,我可以去

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

是否应该在render方法中使用keyPressed(),似乎我想每帧检查一次?dominicbri7如何使用KeyListener接口?你用的是输入处理器?它们之间有什么区别吗?@dotty
KeyListener
是一个java(本机)类。而
InputProcessor
是一个GDX类。根据我的经验,当我使用发动机时,总是使用所有发动机。