我开发了一个2D游戏,并使用OrthographicCamera和视口来调整虚拟棋盘的大小,以实际显示的大小。我将图像添加到stage,并使用ClickListener来检测点击。它工作得很好,但当我改变分辨率时,它就不能正常工作(无法检测到正确的参与者,我认为新的和原始的x和y的问题)。有没有办法解决这个问题?
发布于 2012-06-29 02:06:08
您需要将屏幕坐标转换为世界坐标。你的相机可以做到这点。您可以使用两种方法:cam.project(...)和cam.unproject(...)
或者,如果您已经在使用Actor,请不要自己初始化摄像头,而是使用Stage。创建一个舞台并将演员添加到其中。然后,舞台将为您进行坐标转换。
发布于 2015-01-08 18:19:42
我曾经也遇到过这个问题,但最终我得到了可行的解决方案,可以在libgdx中使用SpriteBatch或Stage绘制任何内容。使用正交化相机,我们可以做到这一点。
首先选择一个最适合游戏的恒定分辨率。这里我拍摄了1280*720(风景)。
class ScreenTest implements Screen{
final float appWidth = 1280, screenWidth = Gdx.graphics.getWidth();
final float appHeight = 720, screenHeight = Gdx.graphics.getHeight();
OrthographicCamera camera;
SpriteBatch batch;
Stage stage;
Texture img1;
Image img2;
public ScreenTest(){
camera = new OrthographicCamera();
camera.setToOrtho(false, appWidth, appHeight);
batch = new SpriteBatch();
batch.setProjectionMatrix(camera.combined);
img1 = new Texture("your_image1.png");
img2 = new Image(new Texture("your_image2.png"));
img2.setPosition(0, 0); // drawing from (0,0)
stage = new Stage(new StretchViewport(appWidth, appHeight, camera));
stage.addActor(img2);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.end();
stage.act();
stage.act(delta);
stage.draw();
// Also You can get touch input according to your Screen.
if (Gdx.input.isTouched()) {
System.out.println(" X " + Gdx.input.getX() * (appWidth / screenWidth));
System.out.println(" Y " + Gdx.input.getY() * (appHeight / screenHeight));
}
}
//
:
:
//
}
在任何类型的分辨率下运行这段代码,它将在该分辨率下进行调整,而不会有任何干扰。
发布于 2012-07-01 15:52:52
我只是觉得Stage很容易使用。如果有一些错误,我认为你应该检查你的代码:
public Actor hit(float x, float y)https://stackoverflow.com/questions/11231451
复制相似问题