这可能是一个愚蠢的问题,但我需要一些帮助/解释。当我在libgdx中调整窗口大小时,我无法正确调整纹理大小。我想保留纹理的方面,或者让它们至少在较小的屏幕尺寸上缩小,就像在移动设备上一样。这是我的代码示例,我的原始窗口大小是600 * 700我尝试了很多东西,但没有任何作用:\
你能帮帮我吗?提前谢谢。public class GameScreen implements Screen {
private World world;
private int ppxX, ppxY;
private SpriteBatch batch;
private OrthographicCamera camera;
private float cameraX, cameraY;
public GameScreen(World world) {
this.world = world;
camera = new OrthographicCamera();
}
public void show() {
batch = new SpriteBatch();
}
@Override
public void render(float delta) {
world.update(delta);
updateCamera();
ppxX = Gdx.graphics.getWidth() / 600;
ppxY = Gdx.graphics.getHeight() / 700;
Gdx.gl.glClearColor(0f, 0f, 0f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(world.getTexture(), 0, 0, ppxX, ppxY);
for (GameElement e : world.getListElements()) {
e.update(delta);
batch.draw(e.getTexture(), e.getPositionX()*ppxX, e.getPositionY()*ppxY, e.getWidth()*ppxX , e.getHeight()*ppxY );
}
batch.end();
}
答案 0 :(得分:2)
你走在正确的轨道上,但有几件事你需要改变。
this.camera = new OrthographicCamera();
this.viewport = new FitViewport(WORLD_SIZE_X, WORLD_SIZE_Y, this.camera);
this.batch = new SpriteBatch();
this.batch.setProjectionMatrix(this.camera.combined);
正确调整大小。
@Override
public void resize(int width, int height) {
this.viewport.update(width, height);
}
更新相机并使用世界单位进行渲染。不要在屏幕像素中思考错误。
public void render(float delta) {
this.camera.update();
this.batch.setProjectionMatrix(this.camera.combined);
this.batch.begin();
// draw using WORLD_SIZE
this.batch.end();
}