繁体   English   中英

如何在Libgdx中跟踪多个触摸事件?

[英]How to track multiple touch events in Libgdx?

我正在使用Libgdx制作赛车游戏。 我想触摸屏幕的右半边以加快速度,同时在不移除先前的触摸点的情况下再次触摸屏幕左侧的另一个触发点。 我无法检测到以后的触摸点。

我已经搜索并获取了Gdx.input.isTouched(int index)方法,但无法确定如何使用它。 我的屏幕触摸代码是:

if(Gdx.input.isTouched(0) && world.heroCar.state != HeroCar.HERO_STATE_HIT){
    guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
    if (OverlapTester.pointInRectangle(rightScreenBounds, touchPoint.x, touchPoint.y)) {
       world.heroCar.state = HeroCar.HERO_STATE_FASTRUN;
       world.heroCar.velocity.y = HeroCar.HERO_STATE_FASTRUN_VELOCITY;
    }
} else {
    world.heroCar.velocity.y = HeroCar.HERO_RUN_VELOCITY;
}

if (Gdx.input.isTouched(1)) {
    guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
    if (OverlapTester.pointInRectangle(leftScreenBounds, touchPoint.x, touchPoint.y)) {
       world.shot();
    }
}

您将需要使用Gdx.input.getX(int index)方法。 整数index参数表示活动指针的ID。 要正确使用它,你需要遍历所有可能的指针(如果两个人在平板电脑上有20个手指?)。

像这样的东西:

boolean fire = false;
boolean fast = false;
final int fireAreaMax = 120; // This should be scaled to the size of the screen?
final int fastAreaMin = Gdx.graphics.getWidth() - 120;
for (int i = 0; i < 20; i++) { // 20 is max number of touch points
   if (Gdx.input.isTouched(i)) {
      final int iX = Gdx.input.getX(i);
      fire = fire || (iX < fireAreaMax); // Touch coordinates are in screen space
      fast = fast || (iX > fastAreaMin);
   }
}

if (fast) {
   // speed things up
} else {
   // slow things down
}

if (fire) {
   // Fire!
}

另一种方法是设置InputProcessor以获取输入事件(而不是像上面的例子那样“轮询”输入)。 当指针进入其中一个区域时,您必须跟踪该指针的状态(因此如果它离开则可以清除它)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM