如何在LibGDX中使ProgressBar工作?

时间:2014-06-26 15:29:41

标签: java libgdx progress-bar scene2d

我正在尝试了解如何在LibGDX中使用ProgressBar。

我创建了这个栏,但我不知道如何让它奏效。我想复制旋钮,以便在60秒内填充条形图(背景线)。我知道如何管理时间,但ProgressBar类中没有方法用旋钮填充栏。至少,我还没有看到它(或者我不明白怎么样)。这是我的代码:

ProgressBar的代码:

skin = new Skin();
Pixmap pixmap = new Pixmap(10, 10, Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
skin.add("white", new Texture(pixmap));

textureBar = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("barGreen_horizontalMid.png"))));
barStyle = new ProgressBarStyle(skin.newDrawable("white", Color.DARK_GRAY), textureBar);
bar = new ProgressBar(0, 10, 0.5f, false, barStyle);
bar.setPosition(10, 10);
bar.setSize(290, bar.getPrefHeight());
bar.setAnimateDuration(2);
stage.addActor(bar);

我知道我可以使用setValue(float)方法移动旋钮。但我想要的是用旋钮的纹理填充条形。这是一个酒吧的截图和旋钮。

enter image description here

任何人都可以帮我理解这个吗?提前谢谢。

3 个答案:

答案 0 :(得分:16)

我遇到了同样的问题,终于找到了。

您必须为 knobBefore 属性设置样式才能达到您想要的效果。 试试这个:

barStyle = new ProgressBarStyle(skin.newDrawable("white", Color.DARK_GRAY), textureBar);
barStyle.knobBefore = barStyle.knob;
bar = new ProgressBar(0, 10, 0.5f, false, barStyle);

希望这有帮助!

答案 1 :(得分:1)

尝试使用setValue(浮点值)函数。在此之前还要设置stepsize和min / max值。

答案 2 :(得分:0)

当您使用屏幕时,您没有内置的ProgressBar,因此您可以使用ShapeRenderer来绘制您的ProgressBar,如下所示:

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.ui.ProgressBar;
import com.badlogic.gdx.scenes.scene2d.ui.ProgressBar.ProgressBarStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.TimeUtils;

public class LoadingScreen implements Screen {
    private MyGame mGame;
    private BitmapFont bf_loadProgress;
    private long progress = 0;
    private long startTime = 0;
    private ShapeRenderer mShapeRenderer;
    private OrthographicCamera camera;
    private final int screenWidth = 800, screenHeight = 480;

    public LoadingScreen(Game game) {
        mGame = (MyGame) game;
        bf_loadProgress = new BitmapFont();
        bf_loadProgress.setScale(2, 1);
        mShapeRenderer = new ShapeRenderer();
        startTime = TimeUtils.nanoTime();
        initCamera();
    }

    private void initCamera() {
        camera = new OrthographicCamera();
        camera.setToOrtho(false, screenWidth, screenHeight);
        camera.update();

    }

    @Override
    public void show() {
        // TODO Auto-generated method stub

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        showLoadProgress();
    }

    /**
     * Show progress that updates after every half second "0.5 sec"
     */
    private void showLoadProgress() {

        long currentTimeStamp = TimeUtils.nanoTime();
        if (currentTimeStamp - startTime > TimeUtils.millisToNanos(500)) {
            startTime = currentTimeStamp;
            progress = progress + 10;
        }
        // Width of progress bar on screen relevant to Screen width
        float progressBarWidth = (screenWidth / 100) * progress;

        mGame.getBatch().setProjectionMatrix(camera.combined);
        mGame.getBatch().begin();
        bf_loadProgress.draw(mGame.getBatch(), "Loading " + progress + " / " + 100, 10, 40);
        mGame.getBatch().end();

        mShapeRenderer.setProjectionMatrix(camera.combined);
        mShapeRenderer.begin(ShapeType.Filled);
        mShapeRenderer.setColor(Color.YELLOW);
        mShapeRenderer.rect(0, 10, progressBarWidth, 10);
        mShapeRenderer.end();
        if (progress == 100)
            moveToMenuScreen();

    }

    /**
     * Move to menu screen after progress reaches 100%
     */
    private void moveToMenuScreen() {
        mGame.setScreen(new MenuScreen(mGame));
        dispose();
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void hide() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        bf_loadProgress.dispose();
        mShapeRenderer.dispose();
    }

}

这会在屏幕底部绘制一个进度条,并以文本形式显示进度。

希望这会有所帮助:)