我有一个全局变量号,因此它获得了一个函数的传递,但是当它完成时,它应该在函数内部更改该变量并将其传递到外部全局变量进行更新。因此,它继续使用旧的全局变量,而其他一切都在更新并变得更大。var P_Level = 1var E_Level = 1var P_Current = 0var P_Max = 100function P_EXP_Gain(exp) { console.log('P_Max1 is '+P_Max) console.log('P_Current1 is '+P_Current) console.log('P_Level1 is '+P_Level) console.log("") if (exp == undefined || null) { P_Current = (E_Level * 20) + P_Current P_Max = P_Max P_Experience(P_Current, P_Max) } else { P_Current = ((E_Level * 20) + exp) + P_Current P_Max = P_Max console.log('P_Max2 is '+P_Max) console.log('P_Current2 is '+P_Current) console.log('P_Level2 is '+P_Level) console.log("") P_Experience(P_Current, P_Max) }}function P_Experience(P_Current, P_Max) { while (P_Current >= P_Max) { P_Level++ // There is a hidden error that causes one to gain perm level up every time they level up via normal way. P_Current = P_Current - P_Max P_Max = P_Level * 100 console.log("") console.log('P_Max3 is '+P_Max) console.log('P_Current3 is '+P_Current) console.log('P_Level3 is '+P_Level) console.log("") alert('Level Up') }}P_EXP_Gain(80)P_EXP_Gain(80) // notice that current random get extra 100P_EXP_Gain(80)您可以通过 console.log 看到,在 while 循环之前,它的 P_Max 始终为 100,但在那之后,它会正确更改,但该更改永远不会更新为全局 P_Max。我还注意到,在第一个 P_EXP_Gain 之后,在考虑增益之前,函数第一部分中的 P_current 将增加 100。我不知道为什么会发生这种情况。我是不是数学哪里搞错了?https://jsfiddle.net/Necrorifter/Lc18tobw/9/
2 回答
慕码人8056858
TA贡献1803条经验 获得超6个赞
我认为您的函数正在使用传递给函数的参数而不是全局变量。您可以重命名参数,它应该可以工作。
function P_Experience(P_Current_param, P_Max_param) {
while (P_Current >= P_Max) {
P_Level++ // There is a hidden error that causes one to gain perm level up every time they level up via normal way.
P_Current = P_Current - P_Max
P_Max = P_Level * 100
console.log("")
console.log('P_Max3 is '+P_Max)
console.log('P_Current3 is '+P_Current)
console.log('P_Level3 is '+P_Level)
console.log("")
alert('Level Up')
}
如果您绝对需要参数,也许像这样。如果你不需要它们,你可以在你的函数中使用全局变量而不传递它们
添加回答
举报
0/150
提交
取消