简体   繁体   中英

LibGDX dragging a texture

I am experimenting with LibGDX and Java and I am wanting to have a texture move along the x axis according to the users finger position if their finger is touching another texture. Below is my code, which works but only when moving your finger to the right

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;
    }
}

Well, there are a few details to take into account.

1) You should give a bit more of information about your problem.

2) That game.batch.draw(touchBound, touchCord.x, touch.y); with touch.y instead of touchCord.y looks really suspicious. Check it out.

3) As a function of how are you handling those positions and texture sizes, you should not unproject the touch position, or you should assign the texture x accordingly.

4) Also remember the direction of the Y axis. It may be pointing up and you would asking if the touch is inside it with wrong coordinates.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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