我是LibGdx的新手,遇到了输入处理问题。
每当触摸按下时,我的玩家需要发射子弹。 但似乎这个方法只被调用一次...
然后用户必须再次点击才能发射另一颗子弹。
我想在点击时始终发射子弹...
有没有办法处理这种情况?
每当触摸按下时,我的玩家需要发射子弹。 但似乎这个方法只被调用一次...
然后用户必须再次点击才能发射另一颗子弹。
我想在点击时始终发射子弹...
有没有办法处理这种情况?
请看一下 "轮询" 触触摸输入,而不是获取 输入事件。因此,在您的渲染或更新方法中,使用以下代码:
boolean activeTouch = false;
if (Gdx.input.isTouched(0)) {
if (activeTouch) {
// continuing a touch ...
} else {
// starting a new touch ..
activeTouch = true;
}
} else {
activeTouch = false;
}
Gdx.input.getX(0)
等)。按钮和按键也是类似的... - P.T.我使用了连续输入处理
这是通过为您的演员设置标志来完成的,例如:
public class Player{
private boolean firing = false;
private final Texture texture = new Texture(Gdx.files.internal("data/player.png"));
public void updateFiring(){
if (firing){
// Firing code here
Gdx.app.log(TAG, "Firing bullets");
}
}
public void setFiring(boolean f){
this.firing = f;
}
Texture getTexture(){
return this.texture;
}
}
@Override
public void create() {
player= new Player();
batch = new SpriteBatch();
sprite = new Sprite(player.getTexture());
Gdx.input.setInputProcessor(this);
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
// Update the firing state of the player
enemyJet.updateFiring();
batch.end();
}
@Override
public boolean keyDown(int keycode) {
switch (keycode){
case Input.Keys.SPACE:
Gdx.app.log(TAG, "Start firing");
player.setFiring(true);
}
return true;
}
@Override
public boolean keyUp(int keycode) {
switch (keycode){
case Input.Keys.SPACE:
Gdx.app.log(TAG, "Stop firing");
player.setFiring(false);
}
return true;
}
完成
我已经成功解决了这个问题,没有使用连接池的概念。
我有自定义的InputProccesor,其中使用了与P.T.提到的类似的逻辑。 在touchDown时,我启动一个线程来发射子弹并进行一些计算,因为我需要访问Renderer类中的一些方法,所以我必须使用。
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
// process the result, e.g. add it to an Array<Result> field of the ApplicationListener.
shootBullet(screenX, screenY);
}
});