2013-08-28 77 views
0

好日子,Libgdx:基本评分系统

我一直想为我的游戏创建一个简单的评分系统,并遇到了问题。我想知道是否有人可以帮我调试我的代码。首先,我遇到的问题是我的代码重复显示了我当前的分数,但每次输入一个触摸命令时,它都会重叠上一次的当前分数。

我想要我的程序做的是,只要它收到一个触摸命令,它就会添加我的分数,然后在屏幕上打印当前分数。

有人可以帮我调试我的代码,并给我一个简单的指南,这将有助于我构建我的分数系统。

这里是我的代码:

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 

} 

回答

2

画面清晰呈现前财产以后否则会重叠旧数据

@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 

    } 
0
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; 
} 
+0

我不明白这一点。这是同一件事。 –

+0

ell justTouched只给你一个回叫,而isTouched给你一个连续的回叫,直到你触及 –

0

对不起,我不能正确地得到你的问题,你可能会错过这个,

currscore += score; 

因为你刚刚宣布分数而不是currsc矿石,所以这可能会有所帮助。

+0

谢谢,它确实提高了我的代码。但它仍然打印并重叠我以前的打印件。 –

+0

对不起,我不知道。 – saravanakumar