How to move a sprite with the keyboard keys using 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 使用键盘键移动精灵?
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01