[英]My local variable not updating the global var?
I have a global variable number, so it gets a pass to a function, but when it completed, it supposes to change that variable inside the function and pass it outside to the global variable to update.我有一个全局变量号,所以它会传递给一个函数,但是当它完成时,它假设在函数内部更改该变量并将其传递给全局变量以进行更新。 So it continuing to use old global variables while everything else is updating and become bigger.
所以它继续使用旧的全局变量,而其他一切都在更新并变得更大。
var P_Level = 1
var E_Level = 1
var P_Current = 0
var P_Max = 100
function 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 100
P_EXP_Gain(80)
You can see via console.log that right before while loop, it always have P_Max of 100, but after that, it change correctly, but that change never update to global P_Max.您可以通过 console.log 看到,就在 while 循环之前,它的 P_Max 始终为 100,但在那之后,它正确更改,但该更改永远不会更新到全局 P_Max。 I also notice that after the first P_EXP_Gain, the P_current will increase by 100 in the first part of the function before it takes into account of gain.
我还注意到,在第一个 P_EXP_Gain 之后,在考虑增益之前,函数的第一部分中的 P_current 将增加 100。 I am at a loss of why this is happening.
我不知道为什么会这样。 Did I get math somewhere wrong?
我是不是在某个地方弄错了数学?
https://jsfiddle.net/Necrorifter/Lc18tobw/9/ https://jsfiddle.net/Necrorifter/Lc18tobw/9/
Your argument names for the P_Experience function are P_Current and P_Max and that is overriding the global scope within the function. P_Experience 函数的参数名称是 P_Current 和 P_Max,这会覆盖函数内的全局作用域。 Easiest solution is to rename the arguments
最简单的解决方案是重命名参数
I think your function is using the parameters you pass into the function instead of your global variable.我认为您的函数正在使用您传递给函数的参数而不是全局变量。 you can rename the parameters and it should work.
您可以重命名参数,它应该可以工作。
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')
}
maybe like this if you absolutely need the parameters.如果你绝对需要这些参数,也许像这样。 if you dont need them you can just use the global variables in your function without passing them in
如果你不需要它们,你可以在你的函数中使用全局变量而不将它们传入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.