Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a ProgressBar work in LibGDX?

I'm trying to understand how to use a ProgressBar in LibGDX.

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. I know how to manage about the time, but there is no method in ProgressBar's class to fill the bar with the knob. At least, I haven't seen it (or I don't understand how, possibly). Here is my code:

ProgressBar's code:

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). But what I want is to fill bar with the knob's texture. Here is a bar's screenshot and the knob.

enter image description here

Can anyone help me to understand this? Thanks in advance.

like image 207
adrianoubk Avatar asked Jun 26 '14 15:06

adrianoubk


2 Answers

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. 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!

like image 53
user3920617 Avatar answered Oct 08 '22 09:10

user3920617


Try to use setValue( float value) function. Also set stepsize and min/max values before that.

like image 35
AcarX Avatar answered Oct 08 '22 09:10

AcarX