This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

my movement code is not replenishing jumps when it touches a tilemap how should i fix this? code is below:

extends KinematicBody2D

const GRAVITY = 10 # in pixels
const JUMP = 250 # start speed px/s
const SPEED = 200 # px/s

var velocity = Vector2.ZERO # (0,0)
var jumps_available := 2    

func _ready():
    pass

func _on_Timer_timeout():
    print(str($Timer.wait_time) + " second(s) finished")

func _physics_process(delta):

    # if no keyboard input for left/right then x speed is 0
    velocity.x = 0 
    if(Input.is_action_pressed("right")):
        velocity.x = SPEED
    elif(Input.is_action_pressed("left")):
        velocity.x = -SPEED

    velocity.y += GRAVITY

# _physics_process
    if Input.is_action_just_pressed("jump") and jumps_available > 0:
        velocity.y -= 250
        jumps_available -= 1
        # _physics_process
        if is_on_floor():
            jumps_available = 2

    velocity = move_and_slide(velocity)

how can i fix this?

Godot version 3.3.3
in Engine by (37 points)

1 Answer

0 votes
      if is_on_floor():
            jumps_available = 2

That part will never be executed, since it's within this other block:

if Input.is_action_just_pressed("jump") and jumps_available > 0:

If jumps are 0, then it's not going to enter this block and refill them, since you are requiring the jumps to be above 0.

I think what you meant to do is this:

    if Input.is_action_just_pressed("jump") and jumps_available > 0:
        velocity.y -= 250
        jumps_available -= 1

    if is_on_floor():
        jumps_available = 2

(Notice the indentation change)

by (1,254 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.