1
\$\begingroup\$

I am making a progress bar for showing the players current experience. However I have come around to a bug, which appears when the player is maxing out his experience and then is getting a new level. In the ideal world my code should reset the progress bar image width (which is a Scene2D image btw), but for some reason it is not setting the image width to zero, but is setting it to its full width instead, which I can't seem to understand why. I hope anyone can help me.

I have provided my code below, and also uploaded a video on youtube with my bug - Video of the bug

The method below is the one I use to change the experience progress bar width as well as the experience.

public void changeLevelExperience(int gainedExperience) {
    float maxExpDividedInPixels = 131f / this.GAME.gameScreen.getGameManager().maxExperience;

    this.GAME.gameScreen.getGameManager().currentExperience += gainedExperience;

    if (this.GAME.gameScreen.getGameManager().currentExperience >= this.GAME.gameScreen.getGameManager().maxExperience) {
        this.GAME.gameScreen.getGameManager().currentLevel++;
        this.currentLevelLabel.setText(this.GAME.gameScreen.getGameManager().currentLevel + "");
        //TODO: Another arrangement for the experience needs to be made.
        this.GAME.gameScreen.getGameManager().maxExperience += 35;
        this.GAME.gameScreen.getGameManager().currentExperience = 0;
        Gdx.app.debug(TAG, this.GAME.gameScreen.getGameManager().currentExperience + "");
    }

    this.levelProgressBar.setWidth((int) (maxExpDividedInPixels * this.GAME.gameScreen.getGameManager().currentExperience * this.scaleFactor));
}
\$\endgroup\$
3
\$\begingroup\$

I haven't personally used ProgressBar yet, but it looks like the right way to set the current percentage is using setValue(float), not setWidth(float). I'd start with that.


Also, saving your GameManager object in a local variable instead of writing this.GAME.gameScreen.getGameManager() 10 times will make the code a lot more legible ;)

\$\endgroup\$
  • \$\begingroup\$ I must have been bad at explaining. The progress bar is not a ProgressBar Widget, but an Image widget. That is why I use setWidth(). :) - I think I have found the bug now. It seems that it was resetting, but it was an image inside of a table, and it was resetting to the table column width, which was set to its current max width. Now it is set to zero every time, because the inital value of the table column width is set to zero. Thanks for you help though.. it is really appreciated :) \$\endgroup\$ – Rohwedder Dec 27 '16 at 10:17
  • \$\begingroup\$ Ah, whoops, I misread the question. Sorry about that! \$\endgroup\$ – Max Dec 30 '16 at 16:43
0
\$\begingroup\$

I found the bug. It was with some help from people at a facebook group. :)

The bug occours, because my Scene2D Image (the level progress bar) was a child of a scene2D Table. And the Table was initially set to have a column width to the width of the image, when it was at its largest size. This width was now set to zero, as I have done in the code below.

table.add(this.levelProgressBar).width(0 * this.scaleFactor).height(10 * this.scaleFactor);

Apparently the Image width is reset to the tables column width,when it is set to the width of zero. Thank you all which tried to help me. It was really appreciated :)

\$\endgroup\$
  • 1
    \$\begingroup\$ That (sort of) makes sense (though it's odd that LibGDX will "helpfully" has arbitrary behavior when setting the image width to 0). You might also just special-case 0 and just hide the image instead. \$\endgroup\$ – Max Dec 30 '16 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.