【问题标题】:libgdx touchDown called just oncelibgdx touchDown 只调用一次
【发布时间】:2023-03-17 17:15:01
【问题描述】:

我是 LibGdx 的新手,输入处理有问题。

每当触地得分时,我的玩家都需要发射子弹。 但是好像这个方法只调用了一次……

然后用户必须再次点击才能射出另一颗子弹。

我希望总是在点击时发射子弹...

有没有办法处理这个用例?

【问题讨论】:

  • 具体是什么方法? (在 Libgdx 中有多种获取触摸事件的方法。)
  • InputProcessor 中的 touchDown 方法。当我单击时……它只被调用一次……无论我是按住单击还是快速释放它。我需要检测用户何时按住点击。有方法 touchDragged ...但仅当我按住单击并移动光标时才调用它...这样我无法检测到用户何时仅按住单击并将光标仍在同一位置...希望您理解这个想法。 ..这很简单,不敢相信没有办法做到这一点。

标签: libgdx


【解决方案1】:

我已经成功地解决了这个问题,没有汇集概念。

我有自定义 InputProccesor,int 我使用过类似的逻辑,如 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);
        }
    });

为了避免 OpenGL 上下文异常。

在touchUp方法中我取消了拍摄线程。

Tnx 的想法!

【讨论】:

  • 不错!很高兴有两个有用的解决方案来解决一个问题。 :)
【解决方案2】:

我用过Continuous Input handle

这是通过为你的演员设置一个标志来完成的,例如:

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

}

现在在我的应用程序类中实现 InputProcessor

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

完成

【讨论】:

    【解决方案3】:

    查看"polling" on the touch input,而不是获取Input events。因此,在您的渲染或更新方法中,使用如下内容:

     boolean activeTouch = false;
    
     if (Gdx.input.isTouched(0)) {
        if (activeTouch) {
           // continuing a touch ...
        } else {
           // starting a new touch ..
           activeTouch = true;
        }     
     } else {
        activeTouch = false;
     }
    

    【讨论】:

    • 好的,这很有道理...但是我需要获取触摸的 X 和 Y 坐标,所以我无法进行旋转和角度的一些计算。如何通过这种方法获得 X 和 Y 坐标?
    • 点击“polling on touch input”链接,您会看到您还可以读取当前的触摸坐标(Gdx.input.getX(0) 等)。类似的按钮和按键...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2014-03-20
    • 1970-01-01
    • 2017-06-08
    相关资源
    最近更新 更多