When I click on this link, I am getting a message “This project is not available for sharing”. Will you try sharing the game again so we can look at the code?
Playing the game I’m still confused as to what is actually supposed to be happening. In general, which your student has correctly, you need two conditions to be true - 'b' pressed and score > 200, and then the code inside works. Does your student want the score to increase without pressing it? setInterval completes a function (which your student have adding to the carrots) every so many milliseconds (which your student has set to 1000 or 1 second).
In other programming languages they have sleep() (Swift) or wait() (Python) to accomplish this. Any more details on what the student is trying to accomplish OR is that just it, when the score is greater than 200 and B button pressed the score will increase by 4 every second - either way, let me know!
The setInterval thing may seem weird at first but the only other way that I can think of starting something and then only doing it every 1sec or after a time interval is a little more involved. And the awesome thing is that y’all had a problem, no one knew how to solve it, and you used your resources to make it work. I think that is awesome and totally what is great about the class and computer science in general.
I would make a variable that would be the amount to add to the carrots. (carrotsToAdd)
I would make a boolean variable that would control whether or not to start adding to the carrots. (startAdding)
I would make a frame count, to count a certain number of frames before adding to the carrots. (this is the 1000 in the setinterval) . (frameCount)
In the if statement, it would make the boolean true, set the amount of carrots to add every time, and then have another if statement outside of the above if statement to check whether to add carrots and actually add the carrots based on whether so many frames had gone by.
if ( startAdding)
{
frameCount++ ;
//60 being 60fps for 60 frames is one second.
if ( frameCount == 60)
{
frameCount = 0 ;
carrots = carrots + carrotsToAdd ;
}
}