簡體   English   中英

Libgdx:基本分數系統

[英]Libgdx: Basic score system

美好的一天,

我一直在嘗試為我的游戲創建一個簡單的分數系統並遇到問題。 我想知道是否有人可以幫我調試我的代碼。 首先我遇到的問題是我的代碼重復顯示我當前的分數,但每次輸入觸摸命令時它都會重疊前一個當前分數。

我希望我的程序要做的是,無論何時收到觸摸命令,它都會添加我的分數,然后在屏幕上打印當前分數。

有人可以幫我調試我的代碼並給我一個簡單的指南,這將有助於我構建我的分數系統。

這是我的代碼:

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

}

在渲染之前清除屏幕,否則它將與舊數據重疊

 @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

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

改為

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

對不起,我無法正確回答您的問題,您可能會錯過這個,

    currscore += score;

因為您之前聲明得分而不是currscore,所以這可能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM