繁体   English   中英

libgdx-将精灵捕捉到光标

[英]libgdx - snap sprite to cursor

我目前正在尝试使用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);

您可以更改光标图像,而不是在鼠标位置绘制Sprite

Gdx.input.setCursorImage(cursorPixMap, hotspotX, hotspotY);

其中cursorPixMap是新光标图像的PixMap ,而hotspotXhotspotYPixMap / cursor图像的“原点”。 在您的情况下,它将是crosshair的中心。
因此,基本上, Gdx.input.getX()Gdx.input.getY()返回hotspotXhotspotY的当前位置。

我建议阅读有关光标的Wiki文章。

暂无
暂无

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

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