use*_*129 3 resize window stage coordinates libgdx
我已经看到很多关于LibGDX和屏幕/凸轮坐标的主题,还有一些关于窗口大小调整的主题,但我找不到解决我遇到的以下问题的方法.
在这个阶段制作基本舞台和基本演员时,比如说windowsize 480x320,一切都还可以.我可以点击我的演员,它会回应.但是当我调整窗口大小时,说600x320,一切看起来都正确,但我的clicklistener不再工作了.此外,舞台坐标被移动或搞砸了.
我使用以下代码:
stage.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
//determine if actor was hit
System.out.println(stage.hit(x, y, true));
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
另外,我正在调整舞台摄像机视口的大小以对应窗口:
stage.getCamera().viewportWidth = Gdx.graphics.getWidth();
stage.getCamera().viewportHeight = Gdx.graphics.getHeight();
Run Code Online (Sandbox Code Playgroud)
因此,在调整大小时,我会在屏幕上获得所需的效果,但是我的听众没有回应 - 演员似乎对我点击的位置有"偏移".我究竟做错了什么?我应该根据调整大小移动我的演员或我的摄像头,还是缩放我的摄像头?有人可以向我解释一下吗?
非常感谢提前!
编辑:下面是我班级的完整代码.
public class HelpMePlease implements ApplicationListener{
// A standard simple Actor Class
class CustomActor extends Actor {
Texture texture = new Texture(Gdx.files.internal("data/testTex2.png"));
TextureRegion pixelTexture = new TextureRegion(texture, 0, 0, 1, 1);
Sprite sprite = new Sprite(texture);
public CustomActor() {
setWidth(128);
setHeight(128);
setBounds(getX(), getY(), getWidth(), getHeight());
}
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
batch.draw(sprite, getX(), getY(), 0f, 0f, getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
}
}
public Stage stage;
public CustomActor actor;
@Override
public void create() {
stage = new Stage(480,320,true);
actor = new CustomActor();
stage.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
//determine if actor was hit
System.out.println(stage.hit(x, y, true));
return true;
}
});
Gdx.input.setInputProcessor(stage);
stage.addActor(actor);
}
@Override
public void resize(int width, int height) {
//resize cam viewport
stage.getCamera().viewportWidth = Gdx.graphics.getWidth();
stage.getCamera().viewportHeight = Gdx.graphics.getHeight();
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
stage.getCamera().update(); //just to be sure, I don't know if this is necessary
stage.act();
stage.draw();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
Run Code Online (Sandbox Code Playgroud)
}
您可以使用最新的夜间更改您的调整大小.
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
Run Code Online (Sandbox Code Playgroud)
最后一个参数"true"将使摄像机在屏幕中居中