The stack is overflowing

how do stop the game from getting stack overflow but still get the outcome I want?ride

2 Likes

Basically, every time you make a sprite move, it checks if it’s overlapping any detected blocks, and runs “On Overlap” events if it is. This means that if you overlap with something and make the sprite move as a result, the code will recalculate the overlap and run another “on overlap” event, before the previous overlap event has finished running. This means that the program quickly fills up with uncompleted overlap events, which are saved in this thing called a “stack”. Once the stack is full, the program errors! To prevent this, we need to stop new overlap events from creating more overlap events.

This is pretty simple to do. We just keep track of if there’s an overlap event happening, and if there is, we prevent new ones, like so:

You’ll also notice I removed those While blocks. Those blocks are loops that don’t stop looping until the condition is false. They also stop all other code from running! This means that if you used those While blocks, the program would freeze entirely until you stopped holding the button! Then those 100s of loops that it did in the split second before you stopped holding the button would all move your sprite up in one giant event and your sprite would be teleported really high in the air.

1 Like