I have an application (game) running in javascript. I used to code Flash games, (and also C and Java years ago), so I am using the canvas to run the game in javascript. There have been many "fun" things I've learned about how javascript works "differently" but I've encountered a problem I can't understand and can't find any reason online.
Javascript will run a function and then (it seems) arbitrarily decide change the input to 0. I say arbitrarily because this functions ran perfectly for the last month, and only today decided to act weird. The function triggers properly, then instantly changes the input that triggered it to 0, and runs all the lines of code in the function. I don't understand how this is happening and nothing I change in my code affects the result. The only solution I've found is deleting the function and writing a new one using some different style... This approach cannot be the best way. Any ideas? Also, this is my first question so I don't know the correct format for asking. My code is thousands of lines long and the function being called is isolated so shouldn't matter.
frameEarring, policeAmount, stoleJewelry are public variables if that matters. the function triggers because earringFrame > 0, but... howCanThisLogicallyEqualZero = 0, getValueOfItem(howCanThisLogicallyEqualZero) = 0, policeAmount = 0, setDanger(50) = 50.
This is the code
if (frameEarring > 0) {
let howCanThisLogicallyEqualZero = frameEarring;
getValueOfItem(howCanThisLogicallyEqualZero);
policeAmount = howCanThisLogicallyEqualZero * 10;
frameEarring = 0, setDanger(50);
stoleJewelry = true; //adds message to phone
}
and here's an image of the game running in chrome96 and showing it
actual code running in console, chrome 96
CodePudding user response:
That should be impossible. The chrome debugger may be confused here. Sometimes the developer tools can do odd things, especially when transpiling and source maps are involved.
Add some logging like console.log('frameEarring before check', frameEarring) before the conditional check, and a console.log('frameEarring is greater than zero', frameEarring) inside the conditional branch. I bet you'll see the values you expect then.
Without a reproducible example or an understanding of your build process, I can't tell you why your debugger is doing that exactly.
Lastly, I'd avoid the comma operator in:
frameEarring = 0, setDanger(50);
if I were you. You probably meant to have a semicolon there anyway, which would make more sense.
