2017-09-16 87 views
0

我想要做的是爲我的世界創建一個虛擬尺寸,並儘可能在屏幕上縮放世界,而不改變寬高比,FitViewport看起來像是最佳人選。多數民衆贊成我如何在舞臺上開始我的視角。libGDX - FitViewport不能很好地調整舞臺

public class PlayStage extends Stage{ 

public static final int  WIDTH = 480; 
public static final int  HEIGHT = 800; 

private final Vector2 gravity = new Vector2(0.f, -9.8f); 
private World   physWorld; 

public PlayStage(){ 
    super(); 
    OrthographicCamera camera = new OrthographicCamera(); 
    camera.setToOrtho(false, WIDTH, HEIGHT); 
    setViewport(new FitViewport(WIDTH, HEIGHT, camera)); 

    physWorld = new World(gravity, false); 
    Gdx.input.setInputProcessor(this); 

    Ball ball = new Ball(physWorld); 
    setKeyboardFocus(ball); 
    addActor(ball); 
} 

@Override 
public void draw() { 
    super.draw(); 
} 

@Override 
public void act(float delta) { 
    super.act(delta); 
    physWorld.step(delta, 10, 5); 
} 

@Override 
public void dispose() { 
    super.dispose(); 
    physWorld.dispose(); 
}} 

這就是精靈在渲染時的樣子(在x座標上縮放得太多)。我的演員也沒有碰到事件。

enter image description here

回答

0

您需要更新階段視創建後更新視口。最好從resize方法調整視口大小。

@Override 
public void resize(int width, int height) { 
    stage.getViewport().update(width,height); 
    //stage.getViewport().update(width,height,true); // -> If you want camera to be at centre. 
} 
0

我解決它在我自己的,所有我必須做的是這樣getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());