繁体   English   中英

LibGDX拖动纹理

[英]LibGDX dragging a texture

我正在尝试使用LibGDX和Java,如果用户的手指触摸另一种纹理,则希望根据用户手指的位置沿着x轴移动纹理。 以下是我的代码,该代码有效,但仅当您向右移动手指时

game.batch.begin();
game.batch.draw(userCar, carCord.x, carCord.y);
game.batch.draw(touchBound, touchCord.x, touchCord.y);
game.batch.end();

if (Gdx.input.isTouched()) {
    Vector3 touchPos = new Vector3();
    Rectangle textureBounds=new Rectangle(touchCord.x,touchCord.y,touchCord.width,touchCord.height);
    touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
    camera.unproject(touchPos);
    if(textureBounds.contains(touchPos.x,touchPos.y) {
        carCord.x = touchPos.x;
    }
}

好吧,这里有一些细节需要考虑。

1)您应该提供有关您的问题的更多信息。

2)该game.batch.draw(touchBound, touchCord.x, touch.y); 使用touch.y而不是touchCord.y看起来真的很可疑。 看看这个。

3)根据如何处理这些位置和纹理大小,您不应该取消投射触摸位置,或者应该相应地分配纹理x。

4)还要记住Y轴的方向。 它可能朝上,您会问触摸是否在坐标内,坐标错误。

希望能帮助到你。

暂无
暂无

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

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