Topic was automatically imported from the old Question2Answer platform.
Asked By
blya
In max_jumps I set the maximum number of jumps. But in the end he jumps 3 times, that is, he will always jump 1 time more than specified in max_jumps. Wtf?
The fact that you get 2 1's in your output is a clue to the problem. Looking at your code, I assume this is what’s happening…
count_jumps starts as 0
You jump…
count_jumps increments to 1 (and prints the first 1 in the output)
Next, you check is_on_floor(). But, that’s only updated after calling move_and_slide, which you haven’t done yet). So, I’d guess that returns true and you reset count_jumps to 0.
Now, you call move_and_slide(), which updates is_on_floor() to return false
Now, you jump again
That increments count_jumps to 1 (again, and prints 1 again)
The next is_on_floor() check (correctly) returns false, so you don’t reset count_jumps. From this point forward, things work as you expect, but you’ve already gotten an extra, first jump due to the incorrect call order of move_and_slide() and is_on_floor().
Bottom line, you need to call move_and_slide()before the result of is_on_floor() is accurate. So, you’ll need to refactor / reorder some of your code.
thank! while I was waiting for an answer, I already guessed this myself, moved the section with the code is_on_floor() higher than the jump check code and everything worked =)