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

How to move a sprite with the keyboard keys using libGDX?(如何使用 libGDX 使用键盘键移动精灵?)

本文介绍了如何使用 libGDX 使用键盘键移动精灵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 java 和 libgdx 并拥有此代码,非常简单,它将精灵打印到屏幕上.这很好用,我从中学到了很多.

i have just started using java and libgdx and have this code, very simply it prints a sprite onto the screen. This works perfectly, and i learnt a lot from it.

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 值?

How would I modify the marioX value when a user presses D on their keyboard?

推荐答案

对于您手头的任务,您甚至可能不需要实现 InputProcessor.你可以像这样在你的 render() 方法中使用 Input.isKeyPressed() 方法.

For your task at hand you might not even need to implement InputProcessor. You can use the Input.isKeyPressed() method in your render() method like this.

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 每秒应沿任意方向行进的像素数.Gdx.graphics.getDeltaTime() 以秒为单位返回自上次调用 render() 以来经过的时间.在大多数情况下,实际上不需要强制转换为 int.

Also note that i made the position coordinates of mario floats and changed the movement to time-based movement. marioSpeed is the number of pixels mario should travel in any direction per second. Gdx.graphics.getDeltaTime() returns you the time passed since the last call to render() in seconds. The cast to int is actually not necessary in most situations.

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

Btw, we have forums at http://www.badlogicgames.com/forum where you ask libgdx specific questions as well!

hth,马里奥

这篇关于如何使用 libGDX 使用键盘键移动精灵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 libGDX 使用键盘键移动精灵?

基础教程推荐