[英]Issue with clamping integer within loop
I'm really at a loss.我真的很茫然。 I've sat here for hours trying to figure out what's wrong but I just can't see it.
我已经坐了好几个小时试图找出问题所在,但我就是看不到它。
So I have an array that holds integers, named xpData[].所以我有一个包含整数的数组,名为 xpData[]。 It's used to keep track of how much XP is needed to "level up".
它用于跟踪“升级”需要多少 XP。 It's a member variable of a class Player.
它是 class 播放器的成员变量。 I utilize this within a member function of that same class named AddXP().
我在名为 AddXP() 的同一个 class 的成员 function 中使用它。
Within said function, I add the function argument to the member variable xp.在所述 function 中,我将 function 参数添加到成员变量 xp。 This works perfectly fine.
这工作得很好。 It even clamps when hitting the maxExp integer.
它甚至在击中 maxExp integer 时会夹紧。 I've tried the function with just these statements as well, and the problem was "solved" (although it created another problem).
我也用这些语句尝试了 function,问题得到了“解决”(尽管它产生了另一个问题)。
The problem is that I go on to, within the same function, utilize a while loop that checks the xp variable against the xpData[lvl] element.问题是我在 go 上,在同一个 function 中,使用一个 while 循环来检查 xp 变量与 xpData[lvl] 元素。 Whenever the xp of the Player is greater than or equal to xpData[lvl], it increments the lvl variable.
每当 Player 的 xp 大于或等于 xpData[lvl] 时,它都会增加 lvl 变量。 The issue is that there are a limited number of levels.
问题是级别数量有限。 The maximum being 30, and the minimum 1. So I utilize a clamping function.
最大值为 30,最小值为 1。所以我使用了钳位 function。 Below is the code:
下面是代码:
GeneralFunctions.cpp通用函数.cpp
int ClampInt(int n, int lower, int upper)
{
int updatedNum = n;
if (n <= lower)
updatedNum = lower;
else if (n >= upper)
updatedNum = upper;
return updatedNum;
}
Player.h播放器.h
class Player
{
public:
Player();
void AddXP(int amount);
int GetLevel() const { return lvl; };
int GetXP() const { return xp; };
private:
int minLevel;
int maxLevel;
int lvl;
int maxExp;
int xp;
int xpData[31] = {
0, // zero-based
10, // Level 1
20, // Level 2
30, // Level 3
40, // Level 4
50, // Level 5
60, // Level 6
70, // Level 7
80, // Level 8
90, // Level 9
100, // Level 10
110, // Level 11
120, // Level 12
130, // Level 13
140, // Level 14
150, // Level 15
160, // Level 16
170, // Level 17
180, // Level 18
190, // Level 19
200, // Level 20
210, // Level 21
220, // Level 22
230, // Level 23
240, // Level 24
250, // Level 25
260, // Level 26
270, // Level 27
280, // Level 28
290, // Level 29
300 // Level 30
};
};
Player.cpp播放器.cpp
Player::Player()
{
minLevel = 1;
maxLevel = 30;
lvl = 1;
maxExp = 1000000;
xp = 0;
}
void Player::AddXP(int amount)
{
if (xp + amount > maxExp)
xp += (maxExp - xp);
else
xp += amount;
while (xp >= xpData[lvl])
{
lvl = ClampInt(lvl + 1, minLevel, maxLevel);
}
}
The issue is that when the xp reaches above the xp required for "level 30", the program just kinda... freezes.问题是,当 xp 达到“30 级”所需的 xp 以上时,程序就有点……冻结了。 No errors, no crashes, nothing.
没有错误,没有崩溃,什么都没有。 Just freezing.
只是冻结。 I assume is has something to do with the loop obviously, but I have no clue what to do to fix it, without giving up much needed functionality (ie clamping the levels, or checking within a loop in case the xp added is ever enough to level the player up multiple times, etc.).
我认为显然与循环有关,但我不知道该怎么做才能修复它,而不会放弃很多需要的功能(即限制级别,或者在循环中检查以防添加的 xp 足以多次升级玩家等)。
Thanks for any help!谢谢你的帮助!
Within the loop循环内
while (xp >= xpData[lvl])
{
lvl = ClampInt(lvl + 1, minLevel, maxLevel);
}
the ClampInt function limits lvl to 30 and the while condition keeps beeing satisfied so it loops forever. ClampInt function 将 lvl 限制为 30 并且 while 条件保持满足,因此它永远循环。
Maybe you should just get rid of the ClampInt function and simply use也许你应该摆脱 ClampInt function 并简单地使用
while(lvl < 30 && xp >= xpData[lvl])
++lvl;
Or you could define a level 31 with an XP of INT_MAX which should not be reached或者您可以定义不应达到的 INT_MAX XP 的级别 31
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.