我在我的相机上使用具有固定纵横比的虚拟屏幕分辨率。初始化看起来像这样:
this.cam = new OrthographicCamera(Config.VIRTUAL_VIEW_WIDTH,
Config.VIRTUAL_VIEW_HEIGHT);
this.cam.setToOrtho(false, Config.VIRTUAL_VIEW_WIDTH,
Config.VIRTUAL_VIEW_HEIGHT);
// create our stage
this.stage = new Stage(0, 0, true);
this.stage.clear(); // always clear the stage too to not get double
// stuff in a stage
stage.setCamera(cam);
//some more stuff
@Override
public void resize(int width, int height) {
// this.cam.setToOrtho(false, width, height);
// calculate new viewport
float aspectRatio = (float) width / (float) height;
float scale = 1f;
Vector2 crop = new Vector2(0f, 0f);
if (aspectRatio > Config.ASPECT_RATIO) {
scale = (float) height / (float) Config.VIRTUAL_VIEW_HEIGHT;
crop.x = (width - Config.VIRTUAL_VIEW_WIDTH * scale) / 2f;
} else if (aspectRatio < Config.ASPECT_RATIO) {
scale = (float) width / (float) Config.VIRTUAL_VIEW_WIDTH;
crop.y = (height - Config.VIRTUAL_VIEW_HEIGHT * scale) / 2f;
} else {
scale = (float) width / (float) Config.VIRTUAL_VIEW_WIDTH;
}
float w = (float) Config.VIRTUAL_VIEW_WIDTH * scale;
float h = (float) Config.VIRTUAL_VIEW_HEIGHT * scale;
viewport = new Rectangle(crop.x, crop.y, w, h);
}
渲染本身很短:
@Override
public void render(float delta) {
cam.update();
// setting the viewport
Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
(int) viewport.width, (int) viewport.height);
// the following code clears the screen with the given RGB color (black)
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// update and draw the stage actors
stage.act(delta);
stage.draw();
这真的很好,我可以使用我的虚拟分辨率。但是我遇到了触摸板的问题。我只是这样初始化它:
Gdx.input.setInputProcessor(stage);
skin = new Skin();
skin.add("knob", new Texture("touchpad/touchKnob.png"));
skin.add("background", new Texture("touchpad/touchBackground.png"));
style = new TouchpadStyle();
// skin.add
style.knob = skin.getDrawable("knob");
style.background = skin.getDrawable("background");
pad = new Touchpad(10, style);
pad.setBounds(0, Config.VIRTUAL_VIEW_HEIGHT - 150, 150, 150);
this.stage.addActor(pad);
如果屏幕不适合虚拟屏幕,它不会对触摸做出正确反应,甚至不会做出反应,因为垫子位于左上边缘,但实际上位于虚拟凸轮上。
任何建议如何解决?
这是它的图片。您看到的区域永远不会改变,因此您始终可以看到整个地图。我为触摸板“所在”的区域着色。菜单中的按钮也有同样的问题。