2014-04-04 69 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