I have a bug in my program that causes the Chip generation to increase exponentially, but I can’t seem to find it. Would you mind going through my code and telling me what’s wrong. You may need to try the game to see the problem for your self. It’ll make more sense then reading it on here
You call generation in two separate functions, and within generation you have setInterval, so you likely have multiple intervals running at the same time. I don’t see a definition for token, and I’m also wondering why you’re using the string"true" instead of the booleantrue. You might want to keep track of whether or not that interval is already running and see if that makes your clicker work as expected.
Well, a simple version could look something like this:
// store the interval (enables you to clear it later if neccessary)
let interval = null;
// remember if the interval is currently running or not
let intervalRunning = false;
// start the interval
function runInterval() {
// if the interval is already running
if (intervalRunning) {
// exit
return;
}
// remember that we are running the interval
intervalRunning = true;
// start the interval
interval = setInterval(function() {}, 1000);
}
// stop the interval
function stopInterval() {
// if the interval is not already running
if (!intervalRunning) {
// exit
return;
}
// remember that we have stopped the interval
intervalRunning = false;
// stop the interval
clearInterval(interval);
// interval doesn't exist anymore
interval = null;
}
Try it out now, I think it’s working the way you expect it to. I actually didn’t do what I suggested, instead I remove all calls to generation and called it immediately after it was defined (I also ignored token), so now when you purchase cookie producers etc it just adds to the base chips per sec of that object rather than starting another loop. What was happening was you were starting another loop each time any of the buttons were pressed and each loop was adding the full amount of base chips per second to the users chips per second so you were getting way more than expected. (That was a pretty rubish explanation, but hopefully you get the gist.)