这是我计算和比较分数的脚本。
void Start()
{
theScore = this;
score = PlayerPrefs.GetInt ("GameScore", 0);
highScore = PlayerPrefs.GetInt("NewHighScore", 0);
}
void Update ()
{
PlayerPrefs.GetInt("Score");
score += Time.deltaTime;
PlayerPrefs.SetInt ("Score", (int)score *100);
}
public void PowerUpScore(int pwrUps)
{
score += pwrUps;
}
void OnGUI()
{
GUI.Label(new Rect(10,10, 100, 30), "Score: " + (int)(score*100));
GUI.Label(new Rect(10,20, 100, 30), "High Score: " + (int)(highScore));
//GUI.Label(new Rect(10,20, 100, 30), "Ammo Left: " + (int)(ammo + 5));
}
public void CheckScore()
{
if (score > highScore)
{
PlayerPrefs.SetInt("NewHighScore", (int)score*100);
}
}
然后要在Update
中对它们进行比较,但它似乎并没有起作用
void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
Vector3 playerPos = playerTransform.position;
yPosition = playerTransform.position.y;
if(yPosition<-9f)
{
Scoring.theScore.CheckScore();
Application.LoadLevel(2);
}
}
如果有人有任何想法,他们会非常感激。谢谢!
答案 0 :(得分:1)
我认为问题在于CheckScore()仅更新PlayerPrefs,而不更新highScore值。试试这个:
public void CheckScore()
{
if (score > highScore)
{
highScore = score;
PlayerPrefs.SetInt("NewHighScore", (int)score*100);
}
}