我一直試圖解決這個問題已經有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維度,但我似乎無法找出一個可能的解決方案。 但我真正不明白的是爲什麼我可以看到陰溝線中的子彈。
好吧,我明白了,但即時閱讀此: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
如果你想使用視口全屏,是的。我的例子把它放在右邊一半。 – Madmenyo
好的。我已經嘗試了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