簡體   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