Essa Rebaño
Essa Rebaño

Reputation: 103

Libgdx: Basic score system

Good day,

I've been trying to create a simple score system for my game and encountered a problem. I was wondering If anyone could help me debug my code for me. First of all the problem which I have encountered is that my code repetitively displays my current score but each time I input a touch command it overlaps the previous current score.

what I wanted my program to do is that whenever it receives a touch command it adds my score and then prints it the current score on the screen.

Can someone please help me in debugging my code and give me a simple guide which will help me in constructing my score system.

here is my code:

Timer time;
SpriteBatch btch;
int score=0,currscore = 0;
BitmapFont fntscore = new BitmapFont(Gdx.files.internal("fonts/pressstartk16white.fnt"),false);

public void score()
{
    if(Gdx.input.isTouched())
    {
        score += 20;
        System.out.print("score: " + score + "\n" );
        currscore = score;
        return;
    }
    else if(Gdx.input.isKeyPressed(Keys.S))
    {
        score +=30;
        System.out.print("score: "+ score + "\n");
        currscore = score;
        return;

    }
}

@Override
public void render(float delta) {

    score();
    btch.begin();
    fntscore.draw(btch, "score: " + currscore, 100, 100);
    btch.end();
    // TODO Auto-generated method stub

}

Upvotes: 0

Views: 1284

Answers (3)

Vikalp Jain
Vikalp Jain

Reputation: 1419

clear screen before rendering somthing otherwise it will overlap old data

 @Override
    public void render(float delta) {
        Gdx.graphics.getGLCommon().glClearColor( 1, 0, 0, 1 );
        Gdx.graphics.getGLCommon().glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
        score();
        btch.begin();
        fntscore.draw(btch, "score: " + currscore, 100, 100);
        btch.end();
        // TODO Auto-generated method stub

    }

Upvotes: 2

saravanakumar
saravanakumar

Reputation: 1777

sorry,i couldn't get your question correctly,you may miss this,

    currscore += score;

Because you declare score just before and not currscore,so this may help.

Upvotes: 0

Kumar Saurabh
Kumar Saurabh

Reputation: 2305

if(Gdx.input.isTouched())
{
    score += 20;
    System.out.print("score: " + score + "\n" );
    currscore = score;
    return;
}

change it to

if(Gdx.input.justTouched())
{
    score += 20;
    System.out.print("score: " + score + "\n" );
    currscore = score;
    return;
}

Upvotes: 0

Related Questions