I am trying to adding a Score in the Game, It should start with Score: 0 and adding 1 to the Score everytime the Object gets hit. The font is working and it shows "score: " [Not the 0] The Collision works.
public class PlayState extends State {
private int score;
private String scoreName;
private BitmapFont font;
public PlayState(GameStateManager gsm) {
super(gsm);
score = 0;
scoreName = "score: 0";
font = new BitmapFont(Gdx.files.internal("font.fnt"));
}
@Override
public void update(float dt) {
handleInput();
if(ObjectA.collides(ObjectB.getBounds()))
scoreName = "score: " + ++score;
}
@Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
font.setColor(0.5f, 0.5f, 0.5f, 0.5f);
font.getData().setScale(0.4f);
font.draw(sb, scoreName, cam.position.x, cam.position.y);
sb.end();
}
ApplicationAdapter class
public class XY extends ApplicationAdapter {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static final String TITLE = "XY";
private GameStateManager gsm;
private SpriteBatch batch;
@Override
public void create () {
batch = new SpriteBatch();
gsm = new GameStateManager();
Gdx.gl.glClearColor(1, 0, 0, 1);
gsm.push(new MenuState(gsm));
}
@Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render(batch);
}
@Override
public void dispose () {
super.dispose();
}
GameStateManager
public class GameStateManager {
private Stack<State> states;
public GameStateManager(){
states = new Stack<State>();
}
public void push(State state){
states.push(state);
}
public void pop(){
states.pop().disposed();
}
public void set(State state){
states.pop().disposed();
states.push(state);
}
public void update(float dt){
states.peek().update(dt);
}
public void render(SpriteBatch sb){
states.peek().render(sb);
}
}
According to your given code, if you're calling update
method from game's render
method then score value continuously increase. score appended string value assign to scoreName when ObjectA.collides(ObjectB.getBounds()
.
After hit increase score by one and pitch new value to scoreName, that used to display your score.
@Override
public void update(float dt) {
handleInput();
if(ObjectA.collides(ObjectB.getBounds())){
score++;
scoreName = "score: " + score;
}
}
But this is not your problem, problem is, font not able to draw score with increased value on screen.
Possible reason may be your font don't have numeric character, so check your font and his .png
file.