根据Libgdx上不同的屏幕分辨率,精灵不合适

时间:2015-02-12 00:28:42

标签: java libgdx screen resolution

我正在测试具有游戏标题的精灵,在我的摩托罗拉Moto G第二代上,精灵的尺寸看起来不错,但我也在母亲的手机上测试,三星GT-S5830i ,精灵的高度看起来很突出。 我也试图理解Viewport的概念(我使用StretchViewport),但我不知道我是否做得对。我的游戏专为移动而非桌面设计。

我在SplashScreen上做到了这一点:

this.gameTitle = new Sprite(new Texture(Gdx.files.internal("images/GameTitle.png")));
this.gameTitle.setSize(Configuration.DEVICE_WIDTH - 50, this.gameTitle.getHeight() * Configuration.DEVICE_HEIGHT / Configuration.DEVICE_WIDTH);

DEVICE_HEIGTH和DEVICE_WIDTH是关于屏幕尺寸的常量。并且" -50"是精灵的边距

在我的视口中,我使用屏幕的实际尺寸作为尺寸,还是应该使用虚拟尺寸?但它是如何工作的?

这是我班级的一部分,我可以改变什么?

// Create the Orthografic camera
this.orthoCamera = new OrthographicCamera(Configuration.DEVICE_WIDTH, Configuration.DEVICE_HEIGHT);
this.orthoCamera.setToOrtho(false, Configuration.VIRTUAL_GAME_WIDTH, Configuration.VIRTUAL_GAME_HEIGHT);
this.orthoCamera.position.set(this.orthoCamera.viewportWidth / 2f, this.orthoCamera.viewportHeight / 2f, 0);
this.orthoCamera.update();

// Combine SpriteBatch with the camera
this.spriteBatch.setTransformMatrix(this.orthoCamera.combined);

// Create the ViewPort
this.viewPort = new ExtendViewport(Configuration.DEVICE_WIDTH, Configuration.DEVICE_HEIGHT);

正如你所说,我将视口更新为ExtendViewport。


主类渲染方法:

public void render() {
    super.render();

    // Update Orthographic camera
    this.orthoCamera.update();

    // Combine SpriteBatch with the camera
    this.spriteBatch.setTransformMatrix(this.orthoCamera.combined);
}

屏幕类渲染方法:

@Override
public void render(float delta) {
    // OpenGL clear screen
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(Gdx.gl.GL_COLOR_BUFFER_BIT | Gdx.gl.GL_DEPTH_BUFFER_BIT);

    // SpriteBatch begins
    this.game.spriteBatch.begin();

    // Display the ClimbUp logo
    this.gameTitle.draw(this.game.spriteBatch);
    this.character.draw(this.game.spriteBatch);

    // SpriteBatch ends
    this.game.spriteBatch.end();
}

1 个答案:

答案 0 :(得分:0)

如果您不希望某些设备上的内容看起来扭曲,并且您不想要黑条(您的客户都不喜欢),则需要使用ExtendViewport而不是StretchViewport。您提供的尺寸应该是基于您想要使用的任何单位的虚拟尺寸。

例如,假设横向游戏,您可以使用800 x 480作为虚拟尺寸,然后您知道该区域内的任何内容(以世界单位)将显示在屏幕上,您可以为此设计游戏。在较窄的设备(4:3比例)上,将显示超过480个垂直单位,在更宽的设备上(16:9比例),将显示超过800个水平单位。

还有另外一个选项可以避免黑条和失真,这就是FillViewport。但我认为一般来说这不是一个好的选择,因为你没有简单的方法来预测你的虚拟尺寸会被裁掉多少。


根据您编辑过的问题,以下是我在代码中要更改的内容:

//No need to create your own camera. ExtendViewport creates its own.

// Pointless to call this now before resize method is called. Call this in render
//XXX this.spriteBatch.setTransformMatrix(this.orthoCamera.combined);

//This is where you give the viewport its minimum virtual dimensions
this.viewPort = new ExtendViewport(Configuration.VIRTUAL_GAME_WIDTH, Configuration.VIRTUAL_GAME_HEIGHT);

//Get reference to the viewport's camera for use with your sprite batch:
this.orthoCamera = (OrthographicCamera) this.viewport.getCamera();

然后在resize方法中:

orthoCamera.setPosition(/*wherever you want it*/);
viewport.update(width, height, false); //these are actual device width and height that are provided by the resize method parameters.

您可能希望将相机相对于视口计算的大小进行定位。然后,您应省略上面的setPosition行,而是在调用viewport.update后计算它。例如,如果你想在屏幕的左下角显示0,0:

viewport.update(width, height, false);
orthoCamera.setPosition(orthoCamera.viewportWidth/2f, orthoCamera.viewportHeight/2f);

在您的渲染方法中,您可以将其放在spriteBatch.begin()

之前
orthoCamera.update();  //good idea to call this right before applying to SpriteBatch, in case you've moved it.
spriteBatch.setProjectionMatrix(orthoCamera.combined);