How to make text/numbers add to the left instead of right? :(

Hi :slight_smile: So I’m pretty sure you’re all confused over the title, so I’ll try to be more clear :stuck_out_tongue:

Im currently adding score to my game, but my problem is that I want to display the Score in the top-right corner, but when I do that, it adds the extra numbers to the right so they go offscreen :frowning: Is there any way to make it put the extra numbers to the left? :confused:

Here’s a pic to explain what I mean :slight_smile:

package com.mayogames.zombiecubes;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.mayogames.zombiecubes.entities.Player;
import com.mayogames.zombiecubes.screens.GameScreen;
import com.mayogames.zombiecubes.weapon.Weapon;

public class HUD {
	
	ZombieCubes zombieCubes;
	private GameScreen gameScreen;
	private Player player;
	private Weapon weapon;
	
	private float stateTime;
	private TextureRegion currentHeartFrame;
	
	public HUD(ZombieCubes zombieCubes, GameScreen gameScreen, Player player, Weapon weapon){
		this.zombieCubes = zombieCubes;
		this.gameScreen = gameScreen;
		this.player = player;
		this.weapon = weapon;
	}
	
	public void tick(){
		
	}
	
	public void render(SpriteBatch hudBatch){
		renderHearts(hudBatch);
		Assets.white.draw(hudBatch, getPointsAsString(gameScreen.getPoints()), 700, 483);
	}
	
	public void renderHearts(SpriteBatch hudBatch){
		stateTime += Gdx.graphics.getDeltaTime();
		currentHeartFrame = Assets.healthHUDAnim.getKeyFrame(stateTime, true);
		
		if(player.getHp() >= 3){
			hudBatch.draw(currentHeartFrame, 64, 448);
			hudBatch.draw(currentHeartFrame, 32, 448);
			hudBatch.draw(currentHeartFrame, 0, 448);
		}else if(player.getHp() == 2){
			hudBatch.draw(Assets.heartEmpty, 64, 448);
			hudBatch.draw(currentHeartFrame, 32, 448);
			hudBatch.draw(currentHeartFrame, 0, 448);
		}else if(player.getHp() == 1){
			hudBatch.draw(Assets.heartEmpty, 64, 448);
			hudBatch.draw(Assets.heartEmpty, 32, 448);
			hudBatch.draw(currentHeartFrame, 0, 448);
		}else if(player.getHp() <= 0){
			hudBatch.draw(Assets.heartEmpty, 64, 448);
			hudBatch.draw(Assets.heartEmpty, 32, 448);
			hudBatch.draw(Assets.heartEmpty, 0, 448);
		}
	}
	
	public String getPointsAsString(int convertToString){
		return String.format("%02d", convertToString);
		
	}
}

Find out how big one character is in pixels. We will call this:


int charlengthinpixels = 4;

Then, you need the length of the number.


int lengthofstring = String.valueOf(score).length;

Now, with every character(lengthofstring) subtract charlengthinpixels from the x value of the score! If the length varies, just add to length in pixels until it is the max!


scorex -= lengthinpixels*lengthofstring;

And thats all!

Easy way: put the score count on the left :wink:
Other way: just experiment with how many pixels to offset. This should have been a problem you could solve on your own.

Just set the offset to account for the highest score you can have.

That’s ugly. No other way, the recalculation is needed.

wouldn’t this work too?


Graphics g = new Graphics();
FontMetrics fm = g.getFontMetrics( g.getFont() );
int scoreWidth = fm.stringWidth(score);

hudBatch.draw(score, 0, 448-scoreWidth);

He uses libgdx, so instead Java’s FontMetrics better use libgdx’s font class (don’t remeber the name, but has methods for calculate width/height).

Draw it backwards on the left and flip it horizontally? ;D

Or you need to calculate the width of the string and draw it offset from the right edge.

Is Swing out of bounds for this question?

A JLabel be set up so that the background color is invisible/transparent. And the text can be right-justified. So, you can set it up for the largest possible score and should not have to worry about it further.

make a offset !!!

I don’t feel like adressing that this has been said, so I’ll quote: