【发布时间】:2017-05-30 21:42:45
【问题描述】:
我正在制作一个 2D 自上而下的益智游戏。我目前在保存高分时遇到了一点问题。我的游戏评分系统是这样工作的:
scoreCount 随着时间(秒)的增加而减少。减少的scoreCount 将被添加到设置为 100 的基本分数中。如果score > highscore,则该分数将被保存为高分。就我而言,一开始的高分应该是0。问题是开头的高分显示随机数 3099(我尝试使用 PlayerPrefs.DeleteAll(); 重置 PlayerPrefs)。这是我的乐谱管理器脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public Text scoreText;
public Text BestScoreText;
private float score;
public float scoreCount;
public float pointsPerSecond;
// void start()
// {
//
//
// }
void Update () {
scoreCount += pointsPerSecond * Time.deltaTime;
score = 100;
}
void FixedUpdate() {
score = score + scoreCount;
scoreText.text = "Your Score : " + Mathf.Round (score);
BestScoreText.text = "Best Score : " + ((int)PlayerPrefs.GetFloat("BestScore"));
//PlayerPrefs.DeleteAll ();
if (score > PlayerPrefs.GetFloat ("BestScore")) {
PlayerPrefs.SetFloat ("BestScore", score);
}
}
}
还有一件事:每当我增加scoreCount 时,默认的最佳分数就会增加。例如,如果我将其设置为 10,则最好的分数显示为 249; 100 表示 1699,200 表示 3099。我犯了什么错误?
【问题讨论】:
-
既然 BestScore 是一个浮点数,为什么要把它转换成一个整数呢?
((int)PlayerPrefs.GetFloat("BestScore")) -
我认为您应该尝试的一件事是将 BestScore 设置为 0 而不是删除 playerprefs
-
只是为了确定,但是你有什么理由在你的更新中把你的分数恢复到 100 而不仅仅是在你的开始功能中设置它?我可能误解了你在那里的分数想要达到的目标。
-
您可能还想在修改屏幕上相应的文本值之前更新您的 PlayerPrefs “BestScore”。这可能不是问题,但它似乎更合乎逻辑。您可以在同一个 if 语句中执行这两项操作,这样您就不会在每个
FixedUpdate时更新您的文本。 -
我认为这完全是由于
FixedUpdate()中的score = score + scoreCount;行,如果FixedUpdate()在两次Update()运行之间运行两次(可能在低帧速率下),那么score将是等于...100 + scoreCount + 100 + scoreCount + Time.deltaTime.