I want to write score inside of a box image.
First I placed the box in the place then I added score string above it using label like this.
private void drawScoreBoxes() {
scoreImage = new Image(scoreTexture);
stage.addActor(scoreImage);
scoreImage.setPosition(UiConstants.BOX_POS_X,UiConstants.BOX_POS_Y2);
}
private void drawScore() {
Label.LabelStyle scoreStyle = new Label.LabelStyle();// style
scoreStyle.font = game.font2;
scoreLabel2 = new Label(scoreController.getScoreString(), scoreStyle);// label
scoreLabel2.setPosition(scoreImage.getX()+scoreImage.getWidth()/2,scoreImage.getY());
}
The score string should come exactly in the middle of the box image.In this case,when number of digit of score increases,string alignment is not proper.
How can I align it center always?
I found methods like getLabelAlign(),setAlignment(alignment) etc. But I don't know how to use it properly.
You have two options: 1. Use TextButton:
Drawable scoreDrawable = new TextureRegionDrawable(new TextureRegion(scoreTexture));
TextButtonStyle textButtonStyle = new TextButtonStyle( scoreDrawable, scoreDrawable, scoreDrawable, font );
TextButton tb = new TextButton( scoreController.getScoreString() );
tb.setDisabled( true );
Or 2. Set Label bounds and align text:
LabelStyle labelStyle = new LabelStyle( font, Color.BLACK );
Label scoreLabel = new Label( scoreController.getScoreString(), labelStyle );
scoreLabel.setBounds( scoreImage.getX(), scoreImage.getY(), scoreImage.getWidth(), scoreImage.getHeight() );
scoreLabel.setAlignment( Align.center );