I wanted to figure this out and avoid making a post, but have been stumped.
I have been trying to program a triple jump like in the Mario series. After testing to find where with the noted-out print statements; the code seems to get stuck running only the first if (console prints ‘jumpCount is 1 now’ every time player jumps, jump height remains the same).
My best guess is jumpCount is not updating. I have tried changing ‘and’ to ‘&&’, changing how it updates (+= instead of ==), changing between between const and var (the current jumpCount is a var = 0), but it still gets stuck on the first if.
If the variable is not being properly updated, what should be changed to make it do so? Otherwise, what is incorrect, or missing in the code?
Theres definitely better ways to do that. But if you’re just looking to fix what you have there. Your first if statement is just handling “ui_accept” so it will never go past that because everytime you hit the button it just does that. If you add and jumpCount == 0 you will pass to the next elif statement on the next jump. Just make sure you set jumpCount to 0 in ready. You will also want to get rid of making jumpCount == 3 and just make it jumpCount = 0 and get rid of your last elif statement. Im on my phone so sorry for not formatting my response correctly.
Along with what normalized said. About = and not == when assigning a value to a variable.
I mistook using == for assigning instead of = due to using += and -= a lot up to that point, admittedly. Thank you for correcting that normalized.
In addition, masteryates6’s suggestion of adding ‘and jumpCount == 0’ to the if on line 23 is what got the code to fully work. That was present in an earlier iteration, but I had removed it. I was confused, and thought it was somehow complicating the following elifs. In reality: The prior issue with trying to check instead of assign (== instead of =) was the underlying issue.
The code now works as intended with check / assign correction alongside the change on line 23.
That said you shouldn’t be doing it like this with multiple ifs. Make it data driven. Here’s a generalized implementation that can handle any number of jumps just by adding speed values into an array
Your first if is always true after pressing the jump button, because it doesn’t have a jumpCount == 0 condition like the rest do.
A cleaner way to do multi-jumps is to make the counter go down, so you give the player a number of jumps, and with each jump the counter goes down until it reaches zero and the player cannot jump anymore. The counter is reset when the player lands. Then in your condition block you can purely check if the player has any remaining jumps, and the jump velocity could use the remaining jumps as some multiplier. This setup also makes it easy to give player additional jumps through powerups.
Why is it “cleaner”? At best it’s irrelevant which way you count. When counting up you don’t need to invert the count if you want to use it as an index into an array of jump velocities.
It may be more personal preference that I find it cleaner. I like to think of it as a “budget” of how many jumps a player has, so counting down and checking if the remaining count is non-zero feels more intuitive to me.
But indeed if you want to attach different effect depending on how many jumps a player has made in a chain, then counting up is more logical.
I suppose the best setup depends on the situation.