Touch code only working at center of screen in my ball game using libgdx

冷暖自知 提交于 2019-12-01 23:47:58

In libgdx, when we draw something on screen then the screen origin is bottom left corner but when we detect touch then screen origin is top left corner.

When you deal with horizontal movement then there is no involvement of y coordinate so it's work fine. You detect touch on ball when your ball is in center then the value of Gdx.input.getY() is approximately same to Gdx.graphics.getHeight()-Gdx.input.getY() so sometimes works.

so simple solution is just invert touch coordinate origin to bottom left corner.

tmp = new Vector3(Gdx.input.getX(),Gdx.graphics.getHeight()-Gdx.input.getY(), 0);

Recommendation :

Don't create object in render method like you're creating object of Rectangle and Vector3, create object in show() or create() method and set value to these variable in render() method.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!