在libGDX InputProcessor的touchDown()中,我正在开发一种在每个屏幕触摸时创建一个新的TouchPoint矩形对象的方法。
我试过对可创建为2的TouchPoint矩形的最大数量设置约束;我只需要使用两个屏幕触摸。应将每个唯一对象更新为touchDragged()中的新屏幕触摸位置,然后在touchUp()上删除。
我已经成功地为单次触摸创建了所需的结果,但不能用于多个屏幕触摸。对于第二次并发触摸,TouchPoint矩形对象从第一次触摸消失,然后在第二触摸位置显示。在第二次触摸的touchUp()时,对象重新出现在第一次触摸的触摸位置。
如何解决?
/*experiment*/
public Vector3 tps[] = new Vector3[2];
/*experiment*/
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
/*experiment*/
if(pointer < 2){
if(pointer == 0) {
Vector3 touchPosition1 = new Vector3();
tps[pointer] = touchPosition1;
tps[pointer].set(screenX, screenY, 0);
gameScreen.getWorldRenderer().getCamera().unproject(tps[pointer]);
touchPoint1 = new TouchPoint(52, 52, new Vector2(tps[pointer].x - 26, tps[pointer].y - 26));
gameScreen.getWorld().getTouchPoints().add(touchPoint1);
Gdx.app.log("TOUCH DOWN", "pointer: " + pointer + " x: " + tps[pointer].x + " y: " + tps[pointer].y);
}
if(pointer == 1) {
Vector3 touchPosition2 = new Vector3();
tps[pointer] = touchPosition2;
tps[pointer].set(screenX, screenY, 0);
gameScreen.getWorldRenderer().getCamera().unproject(tps[pointer]);
touchPoint2 = new TouchPoint(52, 52, new Vector2(tps[pointer].x - 26, tps[pointer].y - 26));
gameScreen.getWorld().getTouchPoints().add(touchPoint2);
Gdx.app.log("TOUCH DOWN", "pointer: " + pointer + " x: " + tps[pointer].x + " y: " + tps[pointer].y);
}
}
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
/*experiment*/
gameScreen.getWorld().getTouchPoints().removeIndex(pointer);
//note: dispose of unused vector3 objects
//note: dispose of all other unused objects
/*experiment*/
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
/*experiment*/
tps[pointer].set(screenX, screenY, 0);
gameScreen.getWorldRenderer().getCamera().unproject(tps[pointer]);
for(TouchPoint tp: gameScreen.getWorld().getTouchPoints()) {
tp.moveRectangleToPosition(new Vector2(tps[pointer].x - 26, tps[pointer].y - 26));
tp.updateRectangleBoundaries();
}
Gdx.app.log("TOUCH DRAGGING", "pointer: " + pointer + " x: " + tps[pointer].x + " y: " + tps[pointer].y);
/*experiment*/
}
此外,如果发生第三次屏幕触摸,程序会崩溃 logcat的:
08-27 14:08:31.808: W/dalvikvm(9988): threadid=11: thread exiting with uncaught exception (group=0x41711438)
08-27 14:08:31.808: E/AndroidRuntime(9988): FATAL EXCEPTION: GLThread 40874
08-27 14:08:31.808: E/AndroidRuntime(9988): java.lang.IndexOutOfBoundsException: 2
08-27 14:08:31.808: E/AndroidRuntime(9988): at com.badlogic.gdx.utils.Array.removeIndex(Array.java:229)
08-27 14:08:31.808: E/AndroidRuntime(9988): at com.manytouches.controller.InputHandler.touchUp(InputHandler.java:104)
感谢您的时间。
答案 0 :(得分:0)
应用支票
如果(指针&2) .....
在触摸和触摸拖动时......你在修饰事件中遇到异常
你也在使用arraylist作为接触点,所以当你在触摸时添加接触点时,它会在列表的最后添加对象,所以这可能是因为......所以在屏幕上创建一个接触点数组而不是数组列表
你可以使代码通用而不是使用if pointer == 1或== 0你可以做的是在inputprocessor创建一个接触点数组并将值赋值为 接触点[指针] .SET(X,Y,Z) 这将使代码更具可读性,因为您在两种条件下都做同样的事情。