Why is the scoring crazy?


#1

will someone look at the code on this and help my students figure out why the scoring is so wacky?
The point of the game is to click on the color that is different. After an incorrect answer the next correct answer adds 2 some 3 instead of 1. I am stumped. Thanks for the help!!


#2

I believe that the problem is that the call to
onEvent(randButtonId …
is inside of the refresh function. So if you happen to set the randomButtonId to a value that has already been used it will register another event handler for the same button. This will result in the event handler getting called twice when the button is clicked. If you continue the game it may even get called 3 or more times per button click as you reuse button IDs.

I think the easiest way to fix this is to move the onEvent call outside of the refresh function and register the handler for all four buttons. Then simply save and check the correct button ID in a global variable when the event executed.

Does that make sense?


#3

Hi @tfrady,

I believe @Mitch is right on and his suggested fix should work. For a more detailed explanation, see here (https://forum.code.org/t/functions-and-event-handlers/15418/2).

Frank