2016-11-19 29 views
0

我目前在libgdx中創建一個stretchViewPort時出現問題。 正如您在圖像中看到的,我添加的遊戲畫面並未延伸到整個屏幕上,並且有黑色空間。我想知道我做錯了什麼。 我提供了下面的代碼的相關部分。此外,我還嘗試使用onport函數中的onport函數,如下所示:m_stage = new Stage(m_viewport);但在這種情況下,我得到了一個黑屏,並沒有呈現任何東西(很難有沒有編譯錯誤)。Libgdx視口不伸展

有人可以指出我做錯了什麼或我失蹤了。 在此先感謝!

親切的問候, Cavasta

Example

package com.block.it; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.PerspectiveCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.scenes.scene2d.InputEvent; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.scenes.scene2d.ui.Image; 
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; 
import com.badlogic.gdx.utils.viewport.StretchViewport; 

public class GameMenu implements Screen { 

    private BlockIt m_blockIt; 
    private Texture m_btexture = new Texture(Gdx.files.internal("mainmenu1.png")); 
    private Image m_background = new Image(m_btexture); 
    private Texture m_starttexture = new Texture(Gdx.files.internal("buttons/c_start.png")); 
    private Image m_startGame = new Image(m_starttexture); 
    private Texture m_optionstexture = new Texture(Gdx.files.internal("buttons/c_controls.png")); 
    private Image m_options = new Image(m_optionstexture); 
    private Texture m_highscoretexture = new Texture(Gdx.files.internal("buttons/c_highscores.png")); 
    private Image m_highscore = new Image(m_highscoretexture); 
    private Texture m_continueTexture = new Texture(Gdx.files.internal("buttons/c_resume_game.png")); 
    private Image m_continue = new Image(m_continueTexture); 
    private Texture m_playerIcon = new Texture(Gdx.files.internal("player/player_move_front.png")); 
    private Image m_player = new Image(m_playerIcon); 
    private int m_buttonPressed = -1; 

    private boolean m_gameInProgress = false; 

    private StretchViewport m_viewport; 
    private PerspectiveCamera m_camera; 
    private Stage m_stage = new Stage(); 

    public GameMenu(BlockIt blockIt, boolean gameInProgress) { 
     m_blockIt = blockIt; 

     m_camera = new PerspectiveCamera(); 
     m_viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), m_camera); 
     m_viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); 
     m_stage = new Stage(); 

    } 

    @Override 
    public void show() { 

     if (!m_gameInProgress) { 
      m_startGame.setX(227); 
      m_startGame.setY(270); 
      m_startGame.addListener(new ClickListener() { 
       @Override 
       public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
        // m_blockIt.startNewGame(); 
       } 

       @Override 
       public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
        // switch to a new texture 
        /*Texture newTexture = new Texture(Gdx.files.internal("buttons/c_new_game.png")); 
        m_startGame.setDrawable(new SpriteDrawable(new Sprite(newTexture))); 
        */ 
        m_player.setX(-64); 
        m_player.setY(300); 
        m_stage.addActor(m_player); 
        m_buttonPressed = 0; 
        return true; 
       } 
      }); 

     } else { 
      m_continue.setX(227); 
      m_continue.setY(270); 
      m_continue.addListener(new ClickListener() { 
       @Override 
       public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
       } 


      }); 

     } 

     m_stage.addActor(m_background); //adds the image as an actor to the stage 

     if (m_gameInProgress) { 
      m_stage.addActor(m_continue); 
     } else { 
      m_stage.addActor(m_startGame); 
     } 

     Gdx.input.setInputProcessor(m_stage); 

    } 

    @Override 
    public void render(float delta) { 
     m_camera.update(); 

     Gdx.gl.glClearColor(0, 0, 0, 1); //sets clear color to black 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //clear the batch 

     m_stage.act(); //update all actors 
     m_stage.draw(); //draw all actors on the Stage.getBatch() 
    } 



    @Override 
    public void resize(int width, int height 
    ) { 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 

    @Override 
    public void hide() { 

    } 

    @Override 
    public void dispose() {  
    } 

} 
+0

嘗試'm_stage =新階段(m_viewport);' – Marius

+0

如果我嘗試,我只是得到了全黑的屏幕甚至沒有顯示任何。 – Cavasta

+1

視口不適用於PerspectiveCamera。使用OrthographicCamera,或編寫一個自定義的Viewport子類。 – Tenfour04

回答

1

你有攝像頭和視口設置爲用於編程世界的價值: 例如。如果你的世界是800 * 480:

camera = new OrthographicCamera(800,480); 
fitViewport = new FitViewport(800, 480, camera); 
+0

這解決了我的問題。謝謝! (將值傳遞給構造函數) – Cavasta

0

必須更新視口時,窗口大小:

@Override 
    public void resize(int width, int height) { 
    m_viewport.update(width, height, true); 
    } 
+0

這並沒有解決它。我仍然有同樣的問題。 – Cavasta

+0

正如Tenfour04所說,使用OrthographicCamera代替。這個對我有用。 – Lordeblader