我创建了自己的InputAdapter,所以我可以在我的3D游戏应用中使用它, 它主要由TouchPad和第一人称相机控制器组成,并计算触摸事件是否触及触摸板或屏幕,以便我可以将触摸事件传递给正确的聆听者,这里是代码:
package com.mygdx.game.test;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
public class MainController extends InputAdapter {
private float YAxis = Gdx.graphics.getHeight();
private PerspectiveCamera camera;
private FirstPersonCameraController cameraController;
private Stage stage;
private int padPointer = -1;
private int camPointer = -1;
private final String TAG = "TOUCH_EVENT";
public MainController(PerspectiveCamera camera, Stage stage) {
this.camera = camera;
this.stage = stage;
cameraController = new FirstPersonCameraController(this.camera);
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Actor actor = stage.hit(screenX, YAxis - screenY, true);
if (actor != null && actor instanceof Touchpad) {
padPointer = pointer;
stage.touchDown(screenX, screenY, pointer, button);
Gdx.app.log(TAG, "pad down pointer = " + pointer);
return true;
} else {
camPointer = pointer;
cameraController.touchDown(screenX, screenY, pointer, button);
Gdx.app.log(TAG, "camera down pointer = " + pointer);
return true;
}
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
if (pointer == padPointer) {
stage.touchDragged(screenX, screenY, pointer);
Gdx.app.log(TAG, "pad dragging pointer = "+pointer);
return true;
}
if (pointer == camPointer) {
cameraController.touchDragged(screenX, screenY, pointer);
Gdx.app.log(TAG, "camera dragging pointer = " + pointer);
return true;
}
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if (pointer == camPointer) {
camPointer =-1;
cameraController.touchUp(screenX, screenY, pointer, button);
Gdx.app.log(TAG, "camera up pointer = " + pointer);
return true;
}
if (pointer == padPointer) {
padPointer = -1;
stage.touchUp(screenX, screenY, pointer, button);
Gdx.app.log(TAG, "pad up pointer = " + pointer);
return true;
}
return true;
}
public void update() {
cameraController.update();
stage.draw();
}
public void dispose() {
stage.dispose();
}
}
这是我的问题的展示案例:
案例1:第一次手指放在触控板上,然后第二次手指放在屏幕上
结果:可以移动触控板,但无法移动屏幕,移动触控板时屏幕也会移动!!
案例2:第一次手指在屏幕上,然后第二次手指在触控板上
结果:它完美地工作,触摸板和屏幕根据手指(指针)独立移动。
所以如何解决第一种情况变得像第二种情况对我来说是完美的。 我不知道cameraController是如何根据第一个指针移动的,即使它不依赖它!?。