【问题标题】:libgdx - how to add background image in stage?libgdx - 如何在舞台中添加背景图片?
【发布时间】:2014-04-27 12:30:58
【问题描述】:

我正在学习 libgdx,但我被困在一个点上..

我在我的舞台上添加了一个按钮,现在我想在舞台上添加一个图像,以便图像看起来像按钮的背景图像。我的意思是说按钮应该位于图像上。 我一直在寻找教程,但无法做到这一点。

怎么做?有什么帮助吗?

【问题讨论】:

  • 顺序很重要,先添加背景。
  • 简单地绘制背景,然后绘制按钮。您可以根据需要将背景作为Imageactor 添加到Stage,或者您可以获取Stages SpriteBatch 并使用它进行绘制。

标签: java android libgdx game-engine scene2d


【解决方案1】:

这是我用来模仿健康条的一些代码。我用了ProgressBar

    stage = new Stage();
    Texture bground = new Texture(Gdx.files.internal("pbBackground.png"));
    Table table = new Table();
    table.setFillParent(true);
    stage.addActor(table);
    font = new BitmapFont(Gdx.files.internal("gamefonts.fnt"));
    font.getData().scale(.1f);
    skin = new Skin();
    pixmap = new Pixmap(1, 1, Format.RGBA8888);
    pixmap.setColor(Color.WHITE);
    pixmap.fill();
    skin.add("white", new Texture(pixmap));
    LabelStyle lstyle = new LabelStyle();
    lstyle.font=font;
    Label mylabel = new Label("HP", lstyle);
    mylabel.setColor(Color.RED);
    mylabel.setPosition(1, 6);
    table.addActor(mylabel);
    textureBar = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("pb.png"))));
    barStyle = new ProgressBarStyle(skin.newDrawable("white", Color.DARK_GRAY), textureBar);
    barStyle.background = new TextureRegionDrawable(new TextureRegion(bground));
    barStyle.knobBefore=barStyle.knob;
    bar = new ProgressBar(0, 10, 0.5f, false, barStyle);
    bar.setPosition(1, 1);
    bar.setValue(0);
    bar.setAnimateDuration(2);
    table.addActor(bar);

这个website 可以帮助您更好地理解进度条。另请查看ProgressBar 的文档。

【讨论】:

    【解决方案2】:

    一个小例子:

    stage.act(Gdx.graphics.getDeltaTime());
    
    stage.getBatch().begin();
    stage.getBatch().draw(background, 0, 0, WORLD_WIDTH, WORLD_HEIGHT);
    stage.getBatch().end();
    
    stage.draw();
    

    【讨论】:

      【解决方案3】:

      你唯一需要做的就是draw背景,然后再绘制Buttons。
      有几种可能的方法可以做到这一点:
      - 您可以将背景作为ImageActor 的子类)添加到Stage,并使用z-index 确保它被绘制为背景。
      - 在调用stage.draw()之前,您可以获取Stages SpriteBatch (stage.getBatch()) 并使用其draw 方法之一绘制背景。

      你也可以使用另一个SpriteBatch,但我不推荐它,因为它是一个相当“重”的对象,在这种情况下它不是必需的。
      我想,在调用stage.draw() 之前,您需要先调用spritebatch.end(),因为SpriteBatch 您曾经是draw 的背景。

      【讨论】:

        猜你喜欢
        • 2020-04-17
        • 2014-10-17
        • 1970-01-01
        • 2018-08-14
        • 2013-03-20
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多