简体   繁体   English

libGDX InputProcessor:如何为每个android touch创建一个新对象?

[英]libGDX InputProcessor: How to create one new object per android touch?

Within the libGDX InputProcessor's touchDown() I am developing a way to create a new TouchPoint rectangle object per screen touch. 在libGDX InputProcessor的touchDown()中,我正在开发一种方法来为每次屏幕触摸创建一个新的TouchPoint矩形对象。

I have tried put a constraint on the maximum number of TouchPoint rectangles creatable to two; 我尝试过限制可创建两个的TouchPoint矩形的最大数量。 I only need to work with two screen touches. 我只需要进行两次触摸即可。 Each unique object should be updated to its new screen touch location within touchDragged(), then deleted on touchUp(). 每个唯一对象都应在touchDragged()中更新到其新的屏幕触摸位置,然后在touchUp()上删除。

I have successfully been able to create the desired result for single touches, but not for multiple screen touches. 我已经成功地能够为单点触摸创建所需的结果,但不能为多屏幕触摸创建所需的结果。 For the second concurrent touch, the TouchPoint rectangle object disappears from the first touch, then shows at the second touch location. 对于第二次并发触摸,TouchPoint矩形对象从第一次触摸中消失,然后显示在第二个触摸位置。 Upon touchUp() of the second touch, the object reappears at the touch location of the first touch. 在第二次触摸的touchUp()上,对象重新出现在第一次触摸的触摸位置。

  1. Which piece of code logic contains the error? 哪段代码逻辑包含错误?
  2. How do I fix it? 我如何解决它?

     /*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*/ } 

Furthermore, the program crashes if a third screen touch occurs. 此外,如果发生第三屏触摸,程序将崩溃。
LogCat: 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)

Thanks for your time. 谢谢你的时间。

Apply the check 套用支票

If(pointer<2) ..... If(指针<2).....

At touchup and touch dragged as well... You are getting exception at touch up event 在润饰和拖动时也是如此...润饰事件中出现异常

Also you be using arraylist for touchpoints so when you add touchpoint on touch down it add the objects at the last of the list so this may because of this... So create a array of touchpoints at your screen instead of array list 另外,您正在使用arraylist作为接触点,因此当您在触地上添加接触点时,会将对象添加到列表的最后,因此可能是因为此...所以在屏幕上创建接触点数组而不是数组列表

You can make the code generic instead of using if pointer==1 or ==0 what you can do is create a array of touchpoint at inputprocessor and assign the value as Touchpoint[pointer].set(x,y,z) this will make code more readable as you are doing same things at both the conditions.. 您可以使代码通用,而不是使用如果pointer == 1或== 0的方式,则可以在输入处理器上创建一个接触点数组并将其值分配为Touchpoint [pointer] .set(x,y,z),这将使代码通用。当您在两种情况下都做相同的事情时,使代码更具可读性。

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

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