2013-10-14 53 views
1

這可能是一個簡單的問題,但我無法找到它。我確實希望有人能帶領我走向正確的道路。我的應用程序的設計分辨率爲800x480。爲了在更高分辨率的設備上保持正確的長寬比,我遵循this post,並設法在更大的屏幕(nexus 7)兩側獲得「黑條」(我用藍色進行測試)。不過,舞臺似乎沒有擴大到覆蓋屏幕。請參閱屏幕截圖波紋管,藍色是Gdx.gl.glClearColor(0.5f,1,1,1);黑色矩形(800x480)是實際的雪碧。需要關於Libgdx Scaling的建議

Link to image

我不知道我哪裏錯了。任何幫助深表感謝。代碼如下:

private SpriteBatch batch; 
private Texture splashTexture; 
private Sprite splashSp; 
TextureRegion splashTr; 
Stage stage; 

@Override 
public void create() { 

    stage = new Stage(); 

    batch = new SpriteBatch(); 

    splashTexture = new Texture(Gdx.files.internal("img/splashTexture.png")); 
    splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 

    TextureRegion splashTr = new TextureRegion(splashTexture, 0, 0, 800, 480); 

    splashSp = new Sprite(splashTr); 

    Gdx.app.log("myapp", "Creating game"); 
} 

@Override 
public void render() {  
    Gdx.gl.glClearColor(0.5f, 1, 1, 1); 
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

    stage.draw(); 

    batch.begin(); 
    batch.draw(splashSp, 0, 0); 

    batch.end(); 
} 

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

    Gdx.app.log("myapp", "width:" + width + " height:" + height); 
    Vector2 size = Scaling.fit.apply(800, 480, width, height); 
    int viewportX = (int)(width - size.x)/2; 
    int viewportY = (int)(height - size.y)/2; 
    int viewportWidth = (int)size.x; 
    int viewportHeight = (int)size.y; 
    Gdx.app.log("myapp", "viewportWidth:" + viewportWidth + " viewportHeight:" + viewportHeight); 
    Gdx.app.log("myapp", "viewportX:" + viewportX + " viewportY:" + viewportY); 
    Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight); 
    stage.setViewport(800, 480, true); 

    Gdx.app.log("myapp", "Resizing game"); 
} 
+1

您的'Stage'完全獨立於精靈的渲染,實際上在這裏什麼都不做。將'Sprite'作爲'Actor'添加到'Stage',或者刪除'Stage'並使用'Camera'作爲您的視口並使用'SpriteBatch.setProjectionMatrix(camera.combined)'。 – noone

回答

1

您需要設置攝像頭和舞臺。

首先聲明相機的變量這樣

OrthographicCamera camera; 

然後在創建方法做這

camera = new OrthographicCamera(); 
camera.setToOrtho(false, 800, 480); 
camera.update(); 

mystage = new Stage(800, 480, false); 

和渲染方法更新相機

camera.update(); 

的MEE工作得很好..

+0

謝謝Sandeep。我以前試過你的建議方法,但沒有運氣。然而,我再次重新測試你的建議方法,我想知道爲什麼它不適合我? – Ziiiii