为什么这不显示我的背景图片title.png?

时间:2014-04-04 08:55:24

标签: java image background textures libgdx

提前为新手问题道歉,目前正在尝试学习libGDX,每个教程都建议这应该有效,它正确显示屏幕中央的文本按钮,但它没有显示背景图片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 个答案:

答案 0 :(得分: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();