【问题标题】:Libgdx: Basic score systemLibgdx:基本评分系统
【发布时间】:2013-08-28 20:31:47
【问题描述】:

早安,

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

我希望我的程序做的是,每当它接收到触摸命令时,它都会添加我的分数,然后将当前分数打印在屏幕上。

谁能帮我调试我的代码并给我一个简单的指南,这将帮助我构建我的评分系统。

这是我的代码:

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

}

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    在渲染之前清除屏幕,否则它将与旧数据重叠

     @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
    
        }
    

    【讨论】:

      【解决方案2】:
      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;
      }
      

      【讨论】:

      • 我不明白。是一样的。
      • ell justTouched 只给你回叫一次,而 isTouched 给你连续回叫直到你被触动
      【解决方案3】:

      对不起,我没能正确回答你的问题,你可能会错过这个,

          currscore += score;
      

      因为您在之前声明分数而不是当前分数,所以这可能会有所帮助。

      【讨论】:

      • 谢谢,它确实改进了我的代码。但是它仍然打印并与我以前的打印重叠。
      • 对不起,我不知道。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 2011-10-24
      相关资源
      最近更新 更多