【问题标题】:Highscore showing unexpected values高分显示意外值
【发布时间】: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.

标签: c# unity3d


【解决方案1】:

您应该在游戏开始时再次将所有内容设置为0 或"0"。否则我猜它会从你以前的设置开始。如果您在 score > 0 时删除了 PlayerPrefs。然后它将再次设置 PlayerPrefs 等于 score。

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

我认为这是解决方案,但我还没有测试过,如果我错了,请见谅。

【讨论】:

    【解决方案2】:

    使用局部变量。

    我怀疑 FixedUpdate() 在两个 Update() 帧之间被调用了两次,从而导致了不希望的值,因为它实际上会使实际得分翻倍(之前的得分值,再加上自身,再加上一些 deltaTime)。

    我们可以通过使用 local 变量而不是逐帧修改的变量来解决这个问题。

      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 有效地以明文形式存储,这意味着用户可以破解他们的高分(如果他们愿意)等等。

    但根据所提出的问题,这就是解决方案。

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 1970-01-01
      • 2018-06-28
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      相关资源
      最近更新 更多