2016-03-02 64 views
1

我一直試圖解決這個問題已經有3個星期了。我開始使用libgdx wiki的視口,並在這個論壇中瀏覽關於視口,虛擬視口等的許多問題,並發現我的遊戲的最佳解決方案是擁有2個視口:1 Fitviewport用於我的遊戲渲染不使用舞臺),另一個Screenviewport爲我的用戶界面(使用階段)。 雖然我有一個特定的問題,我認爲涉及DPI導致控件在三星S4上呈現非常小,他們幾乎不可用,但我會做一個單獨的問題。 我的問題是遊戲渲染和libgdx上的FitViewport。 我設置遊戲攝像機及其視這樣的播放屏幕的構造,它實現屏幕:libgdx批次在FitViewport之外繪製

gameCamera = new OrthographicCamera(); 
gameCamera.setToOrtho(false); 
gameViewport = new FitViewport(800, 480,gameCamera); 

然後在渲染方法我申請這樣說:

input(Gdx.graphics.getDeltaTime()); 

update(Gdx.graphics.getDeltaTime()); 

Gdx.gl.glClearColor(0, 0, 0, 1); 
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

gameCamera.update(); 
uiCamera.update(); 
MyGame.batch.setProjectionMatrix(gameCamera.combined); 
gameViewport.apply(); 
MyGame.batch.begin(); 
//DRAW CODE AFTER THIS 
MyGame.batch.end(); 

//apply stage ScreenViewport and draw: 
game.stage.getViewport().apply(); 
game.stage.draw(); 

然後在調整大小的方法我只有這2行:

game.stage.getViewport().update(width,height,true); 
gameViewport.update(width,height); 
gameCamera.position.set(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,0); 

繪圖方法基於Gdx.graphics.getHeight()和Gdx.graphics.getWidth()座標。 我的遊戲世界是一個自頂向下的射手,它使用Gdx.graphics.getWidth()和getHeight()來生成生物並限制玩家的移動。

再次,我的問題是以下幾點: -FitViewport似乎沒有正常工作。當我在我的設備上嘗試它時,視口適合,創建陰溝線,但我不能看到相關的Gdx.graphics座標繪製的球員。 - 當我在桌面上用奇怪的決議進行試駕時,我可以看到子彈在視口外被拉到排水溝線上。我不知道爲什麼會發生這種情況。

我相信我的問題可能是我應該創建我的世界與其他座標不是Gdx.graphics維度,但我似乎無法找出一個可能的解決方案。 但我真正不明白的是爲什麼我可以看到陰溝線中的子彈。

回答

0

我看到很多人爲此付出了努力,我必須承認我已經有點過早掙扎了。但是一旦你理解了這些基礎知識,就很容易實現。

讓我們拿FitViewport,當你初始化這個new FitViewport(float worldWidth, float worldHeight, camera)你指定世界座標。這意味着無論如何,視口將繪製世界的給定部分。

  • 如果你的世界再大也不會繪製所有
  • 如果你的視口是屏幕邊界之外,你不會看到所有
  • 如果你視方面不等於這些世界方面它的繪圖它會拉伸。

我們還沒有設置屏幕邊界又那麼讓我們做的是:

vp.setScreenBounds(Gdx.graphics.getWidth()/2, 0, Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()); 

這是要求screenWidthscreenHeight這represesent屏幕座標。由於每個人都使用不同的屏幕,我們從圖形本身獲取它。你可以猜測它在哪裏繪製視口;)。

無論如何,這個視口new FitViewport(1920/2, 1080)將始終呈現我的世界960 X 1080在屏幕的右半部分。

如果我的世界是.9f x 1.6f大,那麼我可以像這樣初始化vp new FitViewport(.9f, 1.6f, camera)

PS: 在開始繪製之前,不要忘記在render()中應用VP。

+0

好吧,我明白了,但即時閱讀此:void com.badlogic.gdx.utils.viewport.Viewport.setScreenBounds(int screenX,int screenY,int screenWidth,int screenHeight)。你作爲論證給出的東西似乎不符合函數的描述。我不應該通過:(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight())? – rufoDev

+0

如果你想使用視口全屏,是的。我的例子把它放在右邊一半。 – Madmenyo

+0

好的。我已經嘗試了gameViewport.setScreenBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());和gameViewport.setScreenBounds(Gdx.graphics.getWidth()/ 2,0,Gdx.graphics.getWidth()/ 2,Gdx.graphics.getHeight()); gameViewport = new FitViewport(800,480,gameCamera)之後;在PlayScreen的構造函數方法中,它根本沒有改變行爲。也許我錯過了別的東西?順便說一句我使用game.port.apply()之前batch.begin()內render()函數 – rufoDev