gpt4 book ai didi

java - 高分未保存在 Libgdx 中

转载 作者:行者123 更新时间:2023-11-30 01:21:23 25 4
gpt4 key购买 nike

所以我有两种显示分数的位图字体,一种用于当前分数,一种用于高分。当前分数的文本工作正常,但高分的文本不会增加或节省值(value)。

这是我的代码:

 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!

关于java - 高分未保存在 Libgdx 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37052035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com