All Questions

Community

S
Brody Schulke

Double-jumping on the first jump

I went through and updated my code to reflect your solution, however for some reason I wasn't able to double jump. I did some debugging, and noticed that jump() was actually being called twice on the first jump.


When pressing the "jump" key, it looks like jump is getting called twice due to the following code:


# Enter the state. Passes data when switching from one state to another
func enter(msg: Dictionary = {}) -> void:
	move.enter(msg)
	move.acceleration.x = acceleration_x
	
	if "velocity" in msg:
		move.velocity = msg.velocity
		move.max_speed.x = max(abs(msg.velocity.x), move.max_speed_default.x)
	if not "impulse" in msg:
		_jump_count += 1

and

func unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("jump") and _jump_count < max_jumps:
		jump()
	move.unhandled_input(event)

I printed the resulting velocity and confirmed it was being set to 2x what the specified impulse is. I remove the "impulse" in msg check from the first block of code above, and it seems to have set things back to normal.

Is this a versioning issue that you are aware of? I wasn't able to depict any differences between my code and the code shown in the video.

  • Nathan Lovato replied

    Looking at your code, I can't tell what's causing the issue. It's most likely somewhere else.

    Are you sure jump() is being called twice? Did you place a breakpoint (F9) in the jump() method and see it called twice in a row? In such a case, you want to use your debugger to pause execution and inspect the state of the nodes at runtime.

    Here's a difference I can spot in the code I have in the final project on my hard drive, in the enter() method:

    if "impulse" in msg:
    	jump()
    

    But that shouldn't make the difference you're seeing there.

  • S
    Brody Schulke replied

    I had forgot to remove the leading underscore in the unhandled input function on both Move and Air, so it was being called twice when I had the impulse check you provided above. I removed the leading lines, and included the impulse check to jump and everything is working... silly mistake on my part - thanks Nathan!