首页 文章

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

提问于
浏览
0

我正在测试我的精灵有游戏 Headers ,在我的摩托罗拉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

    如果您不希望某些设备上的内容看起来扭曲,并且您不想要黑条(您的客户都不喜欢),则需要使用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);
    

相关问题