简体   繁体   English

如何在LibGDX中使ProgressBar工作?

[英]How do I make a ProgressBar work in LibGDX?

I'm trying to understand how to use a ProgressBar in LibGDX. 我试图了解如何在LibGDX中使用ProgressBar。

I have created the bar but I don't know how to make it works. 我创建了酒吧,但我不知道如何让它成功。 I want to duplicate the knob in order to fill the bar(background line) in 60 seconds. 我想复制旋钮,以便在60秒内填充条形图(背景线)。 I know how to manage about the time, but there is no method in ProgressBar's class to fill the bar with the knob. 我知道如何管理时间,但ProgressBar类中没有方法用旋钮填充栏。 At least, I haven't seen it (or I don't understand how, possibly). 至少,我还没有看到它(或者我不明白怎么样)。 Here is my code: 这是我的代码:

ProgressBar's code: 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);

I know that I can move the knob with the method setValue(float) . 我知道我可以用方法setValue(float)移动旋钮。 But what I want is to fill bar with the knob's texture. 但我想要的是用旋钮的纹理填充条形。 Here is a bar's screenshot and the knob. 这是一个酒吧的截图和旋钮。

在此输入图像描述

Can anyone help me to understand this? 任何人都可以帮我理解这个吗? Thanks in advance. 提前致谢。

I was having the same problem, and finally found out. 我遇到了同样的问题,终于找到了。

You have to set a style for the knobBefore attribute to achieve what you want. 您必须为knobBefore属性设置样式才能达到您想要的效果。 Try this: 尝试这个:

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

Hope this helps! 希望这可以帮助!

Try to use setValue( float value) function. 尝试使用setValue(浮点值)函数。 Also set stepsize and min/max values before that. 在此之前还要设置stepsize和min / max值。

When your using Screens, you don't have the built in ProgressBar available so instead you can use a ShapeRenderer for drawing your ProgressBar like this: 当您使用Screens时,您没有内置的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();
    }

}

This draws a progressbar at the bottom of the screen and also show you the progress in form of text. 这会在屏幕底部绘制一个进度条,并以文本形式显示进度。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM