[英]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.