0
votes

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);
    }
}
2
You need it to display "score: 0" when the score is 0 and "score: 1" when score is 1? - Oussama Ben Ghorbel
yeah, and adding every time +1 when Object A gets hit - Tim272793
what do you mean by it doesn't fit and also what is Not the 0 in this "score: " [Not the 0] - Abhishek Aryan
My Code should say "Show Score: 0" and on every hit add +1 to the Score. But it just Say "Score: " - Tim272793

2 Answers

0
votes

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.

0
votes

You need to pre-increment so the score name will have the updated value.

if(ObjectA.collides(ObjectB.getBounds()))
            scoreName = "score: " + ++score;
            }

Your previous code will always display the scorename one point late of the actual score.

Furthermore, based on your code you need to call always render after calling update on the main otherwise your score won't be updated. Finally please get yourself used to write this.score when you are referring to class attributes inside of the class.