2016-10-02 40 views
0

我目前正在開發一個遊戲,你必須避免小行星。爲了讓遊戲在我使用FitViewport的每個設備上看起來都一樣。不幸的是,我不知何故在頂部和底部得到白色條,而不是黑色條。我的遊戲背景也是白色的,所以看起來有點奇怪。Fit Viewport黑條

GameScreen:

 @Override 
    public void create() 
    { 
     float aspectRatio = (float)Gdx.graphics.getWidth()/(float)Gdx.graphics.getHeight(); 
     cam = new OrthographicCamera(); 
     viewport = new FitViewport(MyGdxGame.WIDTH * aspectRatio, MyGdxGame.HEIGHT, cam); 
     [...] 
    } 


    @Override 
    public void render(SpriteBatch batch) 
    { 
     cam.update(); 
     batch.setProjectionMatrix(cam.combined); 
     batch.begin(); 
     em.render(batch); //render Ship and Asteroids 
     [...] 
    } 


    @Override 
    public void resize(int width, int height) 
    { 
     viewport.update(width, height); 
     cam.position.set(MyGdxGame.WIDTH/2, MyGdxGame.HEIGHT /2, 0); 
    } 

enter image description here

我拖着船舶入白酒吧。

+0

嘗試之前'cam.update加上幾行()',如果你還沒有: 'Gdx.gl.glClearColor(0F,0F,0F,1F)'' Gdx.gl.glClear (GL20.GL_COLOR_BUFFER_BIT)' – Marius

+0

不幸的是,這兩條線的洞背景是黑色的。如果我想創建效果,我認爲我必須使用白色紋理作爲背景。 - 謝謝 - –

+0

http://xyproblem.info/ – eldo

回答

1

LibGDX提供視口作爲處理不同長寬比的更方便的方式。您不必將MyGdxGame.WIDTH與aspectRatio相乘。只需使用MyGdxGame.WIDTH和MyGdxGame.HEIGHT對其進行初始化即可。

此外,在調整大小功能,您可以更改使用視值凸輪位置(而不是使用常量):

cam.position.set(cam.viewportWidth/2, cam.viewportHeight/2, 0); 
0

我發現了一些問題,在你的代碼。對於使用不同屏幕比例處理時的最佳實踐,只需使用填充視口即可。這裏簡單地用fill viewport編輯你的代碼。只需嘗試一次。

  @Override 
     public void create() 
     { 
      float aspectRatio = (float)Gdx.graphics.getWidth()/(float)Gdx.graphics.getHeight(); 


      camera = new OrthographicCamera(); 
      camera.position.set(0, 0, 0); 
      camera.update(); 
      //1280 is the screen width and 800 is screen height 
      camera.setToOrtho(false, 1280, 800); 
      viewPort = new FillViewport(1280, 800, camera); 

     } 


     @Override 
     public void render(SpriteBatch batch) 
     { 

      batch.setProjectionMatrix(camera.combined); 
      batch.begin(); 
      em.render(batch); //render Ship and Asteroids 
      [...] 
     } 


     @Override 
     public void resize(int width, int height) 
     { 
      viewPort.update(width, height); 
     } 

just try with the above code . it will definitely work