2014-04-04 74 views
1

對於這個非常新手的問題,目前試圖學習libGDX,每個教程都暗示這應該工作,它正確地在屏幕中心顯示文本按鈕,但是它不顯示背景圖片title.png。爲什麼這不顯示我的背景圖片title.png?

我玩過很多嘗試,並得到它沒有運氣的工作,任何幫助,你可以給我將不勝感激!

public class MainMenu implements Screen { 

    ZebraGems game; 
    Stage stage; 
    BitmapFont font; 
    BitmapFont blackfont; 
    TextureAtlas atlas; 
    Skin skin; 
    SpriteBatch batch; 
    TextButton button; 
    Texture titleTexture; 
    Sprite titleSprite; 

    public MainMenu(ZebraGems game){ 
     this.game = game; 
    } 

    @Override 
    public void render(float delta) { 
     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
     stage.act(delta); 

     batch.begin(); 
     titleSprite.draw(batch); 
     stage.draw(); 
     batch.end(); 
    } 

    @Override 
    public void resize(int width, int height) { 
     if (stage == null); 
     stage = new Stage (width, height, true); 
     stage.clear(); 

     titleTexture = new Texture("data/title.png"); 

     titleSprite = new Sprite(titleTexture); 
     titleSprite.setColor(1,1,1,0); 
     titleSprite.setSize(480, 800); 
     titleSprite.setPosition (0, 0); 

     Gdx.input.setInputProcessor(stage); 

     TextButtonStyle style = new TextButtonStyle(); 
     style.up = skin.getDrawable("play"); 
     style.down = skin.getDrawable("playpressed"); 
     style.font = blackfont; 

     button = new TextButton("", style); 
     button.setWidth(213); 
     button.setHeight(94); 
     button.setX(Gdx.graphics.getWidth() /2 - button.getWidth() /2); 
     button.setY(Gdx.graphics.getHeight() /2 - button.getHeight() /2); 

     stage.addActor(button); 
    } 

    @Override 
    public void show() { 

     batch = new SpriteBatch(); 
     atlas = new TextureAtlas("data/playButton.pack"); 
     skin = new Skin(); 
     skin.addRegions(atlas); 

     blackfont = new BitmapFont(Gdx.files.internal("data/blackfont.fnt")); 
    } 

    @Override 
    public void hide() { 
     dispose(); 
    } 

    @Override 
    public void pause() { 

    } 

    @Override 
    public void resume() { 

    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     skin.dispose(); 
     atlas.dispose(); 
     stage.dispose(); 
     blackfont.dispose(); 
    } 
} 

回答

1

移動stage.draw()batch.begin()batch.end()的。

此外,如果您使用的是libgdx的最新睡衣版本,則應該將stage.getViewport().update(width, height, true)添加到resize(...)方法中。

此外,你正在做這titleSprite.setColor(1,1,1,0)它將背景精靈的alpha設置爲0,這意味着100%透明=不可見。

取而代之:titleSprite.setColor(1,1,1,1)

在總渲染應該是這樣的:

batch.begin(); 
titleSprite.draw(batch); 
batch.end(); 

stage.act(delta); 
stage.draw(); 
+0

感謝您的幫助,但是當我刪除stage.draw()停止顯示文本按鈕和犯規要麼顯示背景! – dr0ndeh

+1

我沒有告訴你完全刪除它,但不要在批處理開始/結束時調用它... – noone

+0

也不應該在每次調用resize()時創建一個新的Stage。創建一次,並在resize()方法內,使用setViewport(...)調整視口。所以你順便說一句,甚至不需要你自己的SpriteBatch,因爲舞臺有一個本身... – evident