我正在製作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? '((int)PlayerPrefs.GetFloat(「BestScore」))' – Magnetron
我可以認爲你應該嘗試的一件事就是將BestScore設置爲0 – Magnetron
可以肯定的是,但是你有任何理由把你的分數放回100在您的更新,而不是隻是將其設置在您的開始功能?我可能誤解了你想用你的分數達到的目標。 – Isuka