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
キーボードに?
解決
手元のタスクのために、入力プロセッサを実装する必要さえないかもしれません。このような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()は、sonder()の最後の呼び出し以来、数秒で()render()が通過した時間を返します。 INTへのキャストは、ほとんどの状況で実際には必要ありません。
ところで、フォーラムがあります http://www.badlogicgames.com/forum libgdxの特定の質問も尋ねます!
HTH、マリオ
他のヒント
インターフェイスを使用できます 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
}
}
}
ゲームループメソッド(おそらくレンダリングまたは作成)で入力ハンドラーを追加する必要があります(実装 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.javahttp://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
助けてください
私の好みの方法は、チェックする準備ができているすべての標準キーを保存する入力コントローラーを使用することです。
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;
}