簡體   English   中英

Libgdx中不保存高分

[英]High score is not saved in Libgdx

所以我有兩種顯示分數的位圖字體,一種用於當前分數,一種用於高分。 當前分數的文本工作正常,但高分的文本不會增加或保存值。

這是我的代碼:

 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();
        }

我究竟做錯了什么?

游戲管理器類

 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);
            }
        }
        }
    }

偏愛

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");

        }

請注意,首選項是全局初始化的

你應該打電話

preferences.flush();

緊隨其后

preferences.putInteger("highScore", highScore);

所以最終版本應該看起來像

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

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

您還應該刪除行

gameScreen.highScore ++;

來自您的GameManager類 - 高分應僅在您的條件下進行修改。 不要忘記用 0 或當前首選項的值初始化highscore

暫無
暫無

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

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