How do I get a tile to call the next level in a game?

I want to use a tile and function to call the next level in a game every time the player overlaps that tile. Unfortunately, when my player overlaps the tile on first level the game automatically just moves the player to the final level. How do I get this to happen one level at a time?

Here’s the specific code I’m talking about:

Here’s the complete game if you want to see how it works:

Any help is much appreciated.

1 Like

Your issue is with the if statements in nextLevel:

image

The level2 function sets the level variable equal to 2. When that function call completes and moves on to the next if statement its condition also evaluates to true causing level3 to be called! This is a great example for the debugger; if you set a breakpoint at the top of the nextLevel function and step through the code i think you’ll see exactly what’s going on.

To fix it, simply make the second if an else if instead of its own if statement.

2 Likes

Thanks! I switched it to an else if and it works now. I never used the debugger, but I’ll give it a go next time I run into something.

1 Like