Why does the player fly endlessly up?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ShamanHead

When I press ‘W’ button, my player was flies endlessly.
Code:

extends Node

var can_jump = false;

var Settings = {
	'left'    :    'ui_left',
	'right'   :    'ui_right',
	'top'     :    'ui_up',
	'down'    :    'ui_down',
	'speed'   :    200,
	'jump'    :    200,
	'jump_time':   0,
	'max_jump_time':  0.1  #in seconds
	
	}

var velocity = Vector2();
var grav = get_gravity_scale();
var grounded = false;

func _physics_process(delta):
	if Input.is_action_pressed(Settings.left):
		velocity.x = -Settings.speed;
	elif Input.is_action_pressed(Settings.right):
		velocity.x = Settings.speed;
	else:
		velocity.x = 0;
	if Input.is_action_just_pressed(Settings.top) and can_jump and Settings.jump_time < Settings.max_jump_time:
		Settings.jump_time += delta;
		velocity.y -= Settings.jump;
	elif Input.is_action_just_released(Settings.top):
		can_jump = false;
	if grounded:
		can_jump = true;
		Settings.jump_time = 0; 
	else:
		velocity.y += grav;
	set_linear_velocity(velocity);

func _on_Area2D_area_entered(area):
	grounded=true;
	pass
:bust_in_silhouette: Reply From: paolobb4

I think the problem is here:

if Input.is_action_just_pressed(Settings.top) and can_jump and Settings.jump_time < Settings.max_jump_time:
    Settings.jump_time += delta;
    velocity.y -= Settings.jump;
elif Input.is_action_just_released(Settings.top):
    can_jump = false;

You used Input.is_action_just_pressed() therefore Settings.jump_time += delta; is only called once and the time limit for jumping is never reached.

I think you just have to replace Input.is_action_just_pressed() with Input.is_action_pressed() and it should do the trick :wink: