Libgdx中未保存高分

时间:2016-05-05 13:32:54

标签: java android libgdx

所以我有两个显示分数的Bitmap字体,一个用于当前分数,一个用于高分。当前分数的文本工作正常,但高分的文本不会增加或保存值。

这是我的代码:

 if (manager.score > highScore){
            highScore = manager.score;
            preferences.putInteger("highScore", highScore);

            int getHighScore;
            getHighScore = preferences.getInteger("highScore", highScore);
            this.highScoreFont.draw(batch, "" + getHighScore, Gdx.graphics.getWidth() - 1070, Gdx.graphics.getHeight() - 650);
            preferences.flush();
        }

我做错了什么?

GameManager类

 public void checkCollisions(){
        Iterator<Bullet> it = activeBullets.iterator();
        Iterator<BlueMonster> blueMonsterIT = this.blueMonsterArray.iterator();

        Bullet bullet;
        BlueMonster bMonster;

        while (it.hasNext()){
            bullet = it.next();

            while (blueMonsterIT.hasNext()){
            bMonster = blueMonsterIT.next();
            boolean collision = Intersector.overlaps(bullet.getRect(), bMonster.getRect());

            if (collision == true){

                score++;
                gameScreen.highScore ++;

                sound = Gdx.audio.newSound(Gdx.files.internal("eksplozijaZvuk.ogg"));
                sound.play();
                Hit hit = hitPool.obtain();
                hit.init(bullet.getX(), bullet.getY());
                gameStage.addActor(hit);
                activeHitMarkers.add(hit);

                collision = false;
                activeBullets.removeValue(bullet, true);
                bullet.remove();
                bulletPool.free(bullet);

                bMonster.remove();
                blueMonsterArray.removeValue(bMonster, true);
                blueMonsterPool.free(bMonster);

                Timer.schedule(new Timer.Task() {
                    @Override
                    public void run() {
                        Iterator<Hit> it = activeHitMarkers.iterator();
                        while (it.hasNext()){
                            Hit hit = it.next();
                            hitPool.free(hit);
                            hit.remove();
                            activeHitMarkers.removeValue(hit, true);
                        }
                    }
                }, 0.1f);
            }
        }
        }
    }

PREFERENCE

Preferences preferences = Gdx.app.getPreferences("highScore");


  if (manager.score > highScore){
            highScore = manager.score;
            preferences.putInteger("highScore", highScore);
            preferences.flush();
            Gdx.app.log("Prefka", "flushano");

            int getHighScore;
            getHighScore = preferences.getInteger("highScore", highScore);
            highScoreFont.draw(batch, "" + getHighScore, Gdx.graphics.getWidth() - 1070, Gdx.graphics.getHeight() - 650);
            highScore = getHighScore;
            Gdx.app.log("mjau", "mjau");

        }

请注意,首选项已初始化为globaly

1 个答案:

答案 0 :(得分:1)

你应该致电

preferences.flush();

直接

preferences.putInteger("highScore", highScore);

所以最终版本应该是

preferences.putInteger("highScore", highScore);    
preferences.flush();

int getHighScore;
getHighScore = preferences.getInteger("highScore", highScore);

您还应该删除行

gameScreen.highScore ++;
来自GameManager课程的

- 只有在您的情况下才能修改高分。 不要忘记使用0或当前偏好设置值初始化highscore