Libgdx - Add Scores & Display It At Top Left Corner Of The Screen?
Solution 1:
Here is a link to a libGDX users wiki that I found helpful when I had encountered the same question for a Pong remake.
To display the score, the tools that you need are: an int variable to keep score, a String or CharacterSequence variable to help display the score (I'll use a String), and a BitmapFont which is used to display a font type.
First step: declare all of these tools.
privateint score;
privateString yourScoreName;
BitmapFont yourBitmapFontName;
Second step: initialize them in the create method
publicvoidcreate()
score = 0;
yourScoreName = "score: 0";
yourBitmapFontName = new BitmapFont();
Tertiary step: increment the score variable and change the yourScoreName String variable in your collision logic method (when the raindrop overlaps the bucket).
if(raindrop.overlaps(bucket)) {
score++;
yourScoreName = "score: " + score;
dropSound.play();
iter.remove();
Fourth step: In the render() method, set the color of the font and call the draw method on your BitmapFont between spriteBatch.begin and spriteBatch.end.
batch.begin();
yourBitmapFontName.setColor(1.0f, 1.0f, 1.0f, 1.0f);
yourBitmapFontName.draw(batch, yourScoreName, 25, 100);
batch.end();
Play with: the parameters in the font.setColor() method so that you can see them in contrast to your background color, the number parameters in the font.draw() method to get the score related textures displayed in the top left corner (the last two number parameters in the font.draw() method represent the x and y coordinates of the score texture).
Fifth step: run it, then smile.
The same logic will apply to displaying, "GAME OVER." For heuristic purposes, I'll leave the creation of the logic up to you.
Read the documentation in the link to gain a deeper understanding of the magic.
Solution 2:
To display the score you could create a stage.
Stagestage=newStage();
stage = newStage();
stage.setCamera(cam);
stage.setViewport(WIDTH, HEIGHT, false);
Then you'll have to create like a label, and a text font. So you can look that up on the libgdx wiki for details.
Label text;
LabelStyle textStyle;
BitmapFontfont=newBitmapFont();
//font.setUseIntegerPositions(false);(Optional)
textStyle = newLabelStyle();
textStyle.font = font;
text = newLabel("Gamever",textStyle);
text.setBounds(0,.2f,Room.WIDTH,2);
text.setFontScale(1f,1f);
Then just add that to the stage when the game is lost using:
stage.addActor(text);
Post a Comment for "Libgdx - Add Scores & Display It At Top Left Corner Of The Screen?"