2015-02-12 29 views
0

我測試我的精靈,有遊戲標題,在我的摩托羅拉Moto G第二代精靈的尺寸看起來不錯,但我也測試我的母親電話,一部三星GT-S5830i,精靈的高度看起來很平坦。 我也試圖理解Viewport的概念(我正在使用StretchViewport),但我不知道我是否正確。我的遊戲是爲移動設備而非桌面設計的。根據不同的屏幕分辨率在Libdx不適合的精靈

我這樣做是爲了我的閃屏:

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); 
} 

Screen類渲染方法:

@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(); 
} 
+0

它不會使設備的寬度感,高度是Android上的常量。 – Tenfour04 2015-02-12 20:43:38

回答

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(); 

然後在調整大小的方法:

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. 

您可能要定位你的相機與由視口計算的大小的關係。然後,您應該省略上面的一行,並在調用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); 
+0

我更新了我的問題。我永遠不知道我在哪裏放置虛擬尺寸或設備尺寸。 – Jazzguy 2015-02-12 20:30:02

+0

好吧,我這樣做了,但現在我的屏幕顯示全黑(gl清除所有內容)。我沒有提到我有主課(遊戲課)和我的屏幕課程。 – Jazzguy 2015-02-13 00:56:15

+0

如果您修改了Screen類和Game類的整個渲染方法,請將其發佈。 – Tenfour04 2015-02-13 02:46:26