2017-05-29 112 views
0

我正在製作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.我犯了什麼錯誤?

+0

由於BestScore是一個浮點數,爲什麼要將它轉換爲int? '((int)PlayerPrefs.GetFloat(「BestScore」))' – Magnetron

+0

我可以認爲你應該嘗試的一件事就是將BestScore設置爲0 – Magnetron

+2

可以肯定的是,但是你有任何理由把你的分數放回100在您的更新,而不是隻是將其設置在您的開始功能?我可能誤解了你想用你的分數達到的目標。 – Isuka

回答

0

您應該在遊戲開始時將所有內容設置爲0"0"。否則,它會從我以前的設置開始。如果你刪除你的PlayerPrefs而你的score > 0。然後它將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() 
    { 
    BestScoreText.text = "0"; 
    scoreText.text = "0"; 
    scoreCount = 0; 
    score = 0; 
    } 

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

    if (score > PlayerPrefs.GetFloat ("BestScore")) { 
     PlayerPrefs.SetFloat ("BestScore", score); 
    } 
    } 
} 

我認爲這是解決方案,但我沒有測試過,所以很抱歉,如果我錯了。

+0

除了,你知道,從以前的運行中獲得高分條目...... – Draco18s

0

使用局部變量。

我懷疑FixedUpdate()在兩個Update()幀之間被調用兩次,導致不希望的值,因爲它實際上將實際得分加倍(先前得分值加上自身再加上一些deltaTime)。

我們可以通過使用本地變量而不是逐幀修改的變量來修復該問題。

void FixedUpdate() { 
    float scoreCheck = score + scoreCount; 
    scoreText.text = "Your Score : " + Mathf.Round (scoreCheck); 
    BestScoreText.text = "Best Score : " + ((int)PlayerPrefs.GetFloat("BestScore")); 

    //PlayerPrefs.DeleteAll(); 

    if (scoreCheck > PlayerPrefs.GetFloat ("BestScore")) { 
     PlayerPrefs.SetFloat ("BestScore", scoreCheck); 
    } 
    } 

介意,我覺得有設計遊戲比這仍然更好的辦法,因爲現在沒有什麼阻止分數從去下面0,PlayerPrefs有效地存儲在純文本,這意味着用戶可以破解他們的高分(如果他們希望的話)等等。

但是,根據提出的問題,這是解決方案。