更新游戏进度变量
本文关键字:变量 游戏 更新 | 更新日期: 2023-09-27 18:36:11
我正在尝试创建一个名为 progress
的变量,该变量在游戏过程中会更新(它可以上下移动)。它使用 if 条件每 1/10 秒更新一次。
int progress = 0;
int total;
if (timedecimalpairs % 0.1m == 0) { //Always true
if (demandproductionrate >= 100)
progress += 10;
else
progress -= 50;
total = progress;
GUI.Box (new Rect (Screen.width - 220, 277, 50, 25), total.ToString());
}
timedecimalpairs
按顺序假定值为 0.1/0.2/0.3,因此 if 条件始终为 true。问题是我的progress
变量总是假设值为 10 或 -50,当满足第二个 if 条件时,它不会与 10+10+10 等求和。 demandproductionrate
与游戏期间的玩家输入相关联。
你可以帮我吗?
每当在方法中进行变量初始化时,每次调用该方法时都会初始化它们。我认为在您的示例中,每次触发该方法时您都在初始化int progress = 0;
。为了更正此问题,请从方法中删除初始化,并在应用程序开始时实例化它。