本地最高分统一android

时间:2015-02-14 21:15:07

标签: android

大家好我是团结一致并需要帮助,下面是我当前得分和最高分的代码,目前的分数显示在游戏菜单中,但最高分总是0。

using UnityEngine;

使用System.Collections;

公共类SetScore:MonoBehaviour {

public GameObject newRecordText;
public GUIText thisScoreObj ;
public GUIText thisHighScoreObj ;

void Start () {
    int score = PlayerPrefs.GetInt ("currentScore");
    int highScoreOld = PlayerPrefs.GetInt ("highestScoreOld");

    newRecordText.SetActive (false);

    if (score > highScoreOld) {
        newRecordText.SetActive(true);
    }

    thisScoreObj.text = "" + score;
    thisHighScoreObj.text = "" + highScoreOld;

    AdjustFontSize ();
}

void AdjustFontSize() {
    if(Screen.height > 480 && Screen.width > 800){
        thisScoreObj.fontSize = 60;
        thisHighScoreObj.fontSize = 60;
    }

    else if(Screen.height <= 480 && Screen.width <= 800)        {
        thisScoreObj.fontSize = 40;
        thisHighScoreObj.fontSize = 40;
    }
}

}

void start() {
if (PlayerPrefs.HasKey ("highestScore")) {
                    highestScore = PlayerPrefs.GetInt ("highestScore");     
                    highestScoreOld = highestScore;
            } else { 
        highestScore = 0;
        highestScoreOld = highestScore;
    }
}

void gameOver(int score) {
    //Debug.Log ("GAME OVER!!! Your score: " + score);
    Application.LoadLevel("GameOverMenu");
    if (score > highestScore) {
        highestScore = score;       
    }
    PlayerPrefs.SetInt ("currentScore", score);
    PlayerPrefs.SetInt ("highestScore", highestScore);
    PlayerPrefs.SetInt ("highestScoreOld", highestScoreOld);



}

}

2 个答案:

答案 0 :(得分:0)

我认为你应该调用Application.LoadLevel(&#34; GameOverMenu&#34;);在方法的最后。在方法完全完成之前,您正在加载新场景。这是修改过的 -

void gameOver(int score) {
    //Debug.Log ("GAME OVER!!! Your score: " + score);
    if (score > highestScore) {
        highestScore = score;       
    }
    PlayerPrefs.SetInt ("currentScore", score);
    PlayerPrefs.SetInt ("highestScore", highestScore);
    PlayerPrefs.SetInt ("highestScoreOld", highestScoreOld);
    Application.LoadLevel("GameOverMenu");
}

希望它有所帮助。

答案 1 :(得分:0)

我在下面修复它!!!

void gameOver(int score) {
//Debug.Log ("GAME OVER!!! Your score: " + score);
if (PlayerPrefs.HasKey ("highestScore")) {
                highestScore = PlayerPrefs.GetInt ("highestScore");     
                highestScoreOld = highestScore;
        } else { 
    highestScore = 0;
    highestScoreOld = highestScore;
}
if (score > highestScore) {
    highestScore = score;       
}
PlayerPrefs.SetInt ("currentScore", score);
PlayerPrefs.SetInt ("highestScore", highestScore);
PlayerPrefs.SetInt ("highestScoreOld", highestScoreOld);
Application.LoadLevel("LoadLevel");

} }

相关问题