2011-04-29 87 views
10

我刚刚开始使用java和libgdx并拥有此代码,非常简单,它将一个精灵打印到屏幕上。这很好,我从中学到很多东西。如何使用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

回答

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, 马里奥

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; 

因此,实施重点*方法并检查是否存在键同时按下,并从马里奥递增或递减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

希望帮助

+0

怎么来dominicbri7使用KeyListener接口?你使用了InputProcessor?他们之间有什么区别吗? – dotty 2011-04-29 14:37:24

+0

@dotty'KeyListener'是一个java(native)类。而'InputProcessor'是一个GDX类。根据我的经验,当我使用引擎时总是使用所有的引擎。 – jotapdiez 2011-04-29 14:58:26

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

} 
+0

如果keyPressed()在渲染方法中,似乎我想检查这个每一帧? – dotty 2011-04-29 14:30:47

0

我的优选的方法是使用存储了所有标准键准备用于检查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; 
    } 
} 

然后,所有我需要做的是让你的游戏的创建方法KeyboardController与

controller = new KeyboardController(); 

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

Gdx.input.setInputProcessor(controller); 

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

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