[英]Sprites not fitting according to different screen resolutions on Libdx
我正在測試我的精靈有游戲標題,在我的摩托羅拉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();
}
如果您不希望某些設備上的內容看起來扭曲,並且您不想要黑條(您的客戶都不喜歡),則需要使用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);
在你的render方法中,你可以在spriteBatch.begin()
之前放置它:
orthoCamera.update(); //good idea to call this right before applying to SpriteBatch, in case you've moved it.
spriteBatch.setProjectionMatrix(orthoCamera.combined);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.