我目前正在尝试使用libgdx将十字准线精灵捕捉到游戏光标上。游戏是自上而下的视图:

    Texture crosshair_text = new Texture(Gdx.files.internal("data/crosshair1.png"));
    this.crosshair = new Sprite(crosshair_text, 0, 0, 429, 569);

    //...
@Override
public void render() {

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    cam.update();
    batch.setProjectionMatrix(cam.combined);

    batch.begin();

    //.. sprites that scale based on camera (top-down view)

    batch.end();

    //draws 'ui' elements
    floatingBatch.begin();

    //...

    //snap to cursor
    this.crosshair.setPosition( Gdx.input.getX(), (Gdx.graphics.getHeight()-Gdx.input.getY()) );

    //transform
    this.crosshair.setScale(1/cam.zoom);

    //draw
    this.crosshair.draw(floatingBatch);

    floatingBatch.end();
}


抱歉,如果我没有发现错误,这不是我的代码的精确副本。这里的问题是:1.子画面无法捕捉到正确的位置,并且2.十字准线子画面滞后于屏幕上鼠标的当前位置。谁能给我关于如何解决这两个问题的见解?

最佳答案

您的位置可能不正确,因为屏幕位置不一定是使用OrthographicCamera绘制的正确位置,请首先尝试使用unproject。例如:

Vector3 mousePos = new Vector3( Gdx.input.getX(), (Gdx.graphics.getHeight()-Gdx.input.getY()), 0); //Get the mouse-x and y like in your code
cam.unproject(mousePos); //Unproject it to get the correct camera position
this.crosshair.setPosition(mousePos.x, mousePos.y); //Set the position


另外,您需要确保将浮动批次设置为相机的投影矩阵,并添加以下代码:

floatingBatch.setProjectionMatrix(cam.combined);

10-08 02:49