如果我拿出屏幕调整大小问题,我的代码功能齐全。但现在我尝试了包括平板电脑在内的多种设备,分辨率非常差。所以我试图在视口的帮助下保持相同的宽高比。我正在使用舞台来添加我的按钮和图像。我的clicklistener响应部分天沟和部分播放按钮。由于新帐户,Stackoverflow不允许我发布图片。任何人都可以帮助我理解我做错了什么,并确保这段代码适用于手机和桌面。
private static final int VIRTUAL_WIDTH = 480;
private static final int VIRTUAL_HEIGHT = 800;
public void resize(int width, int height) {
// calculate new viewport
Vector2 size = Scaling.fit.apply(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, width, height);
int viewportX = (int)(width - size.x) / 2;
int viewportY = (int)(height - size.y) / 2;
int viewportWidth = (int)size.x;
int viewportHeight = (int)size.y;
Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
}
@Override
public void show() {
batch = new SpriteBatch();
stage = new Stage();
camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
camera.setToOrtho(true, VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
worldCoords = new Vector3( VIRTUAL_WIDTH, VIRTUAL_HEIGHT, 0);
camera.unproject(worldCoords);
camera.update();
white = new BitmapFont(Gdx.files.internal("font/white.fnt"), true);
white.setScale(1f, -1f);
skin = new Skin();
atlas = new TextureAtlas("button.pack.pack");
skin.addRegions(atlas);
buttonStyle = new TextButtonStyle();
buttonStyle.up = skin.getDrawable("button");
buttonStyle.down = skin.getDrawable("buttonpressed");
buttonStyle.font = white;
buttonStyle.fontColor = Color.BLACK;
buttonPlay =new TextButton("Play", buttonStyle);
buttonPlay.setWidth(VIRTUAL_WIDTH/3);
buttonPlay.setHeight(VIRTUAL_HEIGHT/12);
buttonPlay.setPosition(stage.getViewport().getRightGutterWidth(), stage.getViewport().getWorldHeight()/2);
buttonPlay.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
((Game) Gdx.app.getApplicationListener()).setScreen(new MainFlow(g));
}
});
Gdx.input.setInputProcessor(stage);
Image img = new Image(new Texture(Gdx.files.internal("Optimized-BG_2.png")));
img.setFillParent(true);
stage.addActor(img);
stage.addActor(buttonPlay);
}
@Override
public void render(float delta) {
camera.update();
batch.setProjectionMatrix(camera.combined);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
答案 0 :(得分:2)
我不明白你想要什么,但首先尝试更新阶段,在你的resize方法中,并阅读libgdx docs
stage.getViewport().update(width, height, true);