Search code examples
debuggingapp-lab

debugging issues inside applab


I am currently creating a quiz creator on appLab for my CS final and I have most of my code working although some of the onEvent lines of my code keep repeating even though I do not have a loop there. I have called the function a few times inside of my program which might be the reason it is malfunctioning. I have tried refactoring the code and splitting up the function into 2 smaller functions one that handles declaring the variables and one that makes the changes to the screen. The main issue is that the code inside the onEvent loop is repeating so I cannot add a counter to count how many questions the user has made correct or incorrect. I am super confused as I have been trying to fix this bug for the past few days. at first, it seems to be working but when you slow down the code before you click on an answer you will see the issue inside the quiz function in the onEvent lines. Thank you so much for helping me out!

here is a link to my project if you want to reference the code (for some reason the formatting of the code does not work in stack overflow) https://studio.code.org/projects/applab/bzlJBCafMhrd-LAGoqro3nXw8hQ7tJBLxj-N3In9kwo`enter code here`


Solution

  • some of the onEvent lines of my code keep repeating even though I do not have a loop there

    The answer can be found in this Code.org forum thread. The key statement is:

    • When you call onEvent() it creates a new event handler sitting in the background responding to events. Once you’ve created an event handler, there’s no way to get rid of it. When you restart the game the old event handlers are still hanging out in the background and new event handlers are created...

    In your case, restart the game means just going to the next quiz screen, calling the function quiz(), wherein you call onEvent(). In the second run of quiz() a second event handler is created, hence the callback function is called twice, in the third quiz round thrice, and so on. So, to avoid this, don't call onEvent() repeatedly for the same UI element event.