精灵不适合根据 Libdx 上的不同屏幕分辨率
Sprites not fitting according to different screen resolutions on Libdx
我正在测试带有游戏名称的 sprite,在我的摩托罗拉 Moto G 2nd generation 上,sprite 的尺寸看起来不错,但我也在测试我的母亲 phone,三星 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”是精灵的边距
在我的视口中,我使用屏幕的实际尺寸作为尺寸,还是应该使用虚拟尺寸?但它是如何工作的?
这是我主要的一部分class,我可以更改什么?
// 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。
主要class渲染方法:
public void render() {
super.render();
// Update Orthographic camera
this.orthoCamera.update();
// Combine SpriteBatch with the camera
this.spriteBatch.setTransformMatrix(this.orthoCamera.combined);
}
屏幕class渲染方法:
@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();
}
如果您不希望内容在某些设备上看起来失真并且您不想要黑条(none 的客户会喜欢),您需要使用 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);
我正在测试带有游戏名称的 sprite,在我的摩托罗拉 Moto G 2nd generation 上,sprite 的尺寸看起来不错,但我也在测试我的母亲 phone,三星 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”是精灵的边距
在我的视口中,我使用屏幕的实际尺寸作为尺寸,还是应该使用虚拟尺寸?但它是如何工作的?
这是我主要的一部分class,我可以更改什么?
// 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。
主要class渲染方法:
public void render() {
super.render();
// Update Orthographic camera
this.orthoCamera.update();
// Combine SpriteBatch with the camera
this.spriteBatch.setTransformMatrix(this.orthoCamera.combined);
}
屏幕class渲染方法:
@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();
}
如果您不希望内容在某些设备上看起来失真并且您不想要黑条(none 的客户会喜欢),您需要使用 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);