Hey guys, I am working on a virtual pet makecode arcade project. Basically, the goal is to keep feeding the pet and prevent it from dying. However, when I press on the menu button on the simulator, the program goes into an error and stops after a few seconds accessing the menu and interacting with its options. Please tell me how I can solve this problem. Herre’s the link to my code:
I think that you had an undefined warning in a block that modifies a health bar.
I fixed it using the very useful sprite utils extension:
jwunderl/arcade-sprite-util
Here, tell me if it still crashes:
It seems the ‘change statusbar value’ block in the ‘forever’ loop causes an error when it tries to run when the menu is open. This is because when the game is PAUSED in the menu, all sprites are set to null* so you can’t perform any operations (Like changing a value) on them.
Usually, the forever block wouldn’t cause this error, because the PAUSE menu would automatically ignore anything inside to avoid the error.
However, the ‘pause 10,000 ms’ means that when the game is UNPAUSED, the forever block will run and then wait 10 seconds before changing the value. Once the wait is over, the code will try to run when the game is PAUSED, and result in an error because it can’t find the sprites you’re trying to change the value of.
To fix this: Move the ‘change statusbar value’ block to before the pause, so that it doesn’t accidentally run while paused.
However, this means when the ‘forever’ block runs at the start of the game, the pet will immediately lose 10 health. To fix this, I added a ‘first’ boolean so that on the first time the loop runs, the block to take damage is ignored.
Fixed code:https://arcade.makecode.com/S17549-23251-11348-39743
I hope I explained this right
*I think- I’m not entirely sure, but this is what debug says.