Topic was automatically imported from the old Question2Answer platform.
Asked By
Juwu
I put a ‘space’ input for the player, this input is to shoot a projectile and he work fine when the player’s moving , but when I specifically press arrow up + arrow left + space or arrow down + arrow right + space, the space input stop to work and the projectile not pops up, this happens just when I press this keys
the code below
extends Node2D
var speed = 7
var tiro = true
var up = false
var down = false
var left = false
var right = false
var proj = preload('res://Scenes/Ball.tscn')
var tick = 0
func time():
tick += 1
if tick == 30:
tick = 0
tiro = true
func _process(delta):
move()
stop()
if tiro == false:
time()
pass
func move():
if Input.is_action_pressed("ui_right"):
position.x += speed
pass
if Input.is_action_pressed("ui_left"):
position.x -= speed
pass
if Input.is_action_pressed("ui_up"):
position.y -= speed
pass
if Input.is_action_pressed("ui_down"):
position.y += speed
pass
if Input.is_action_just_pressed("ui_select") and tiro == true:
print('fire')
tiro = false
var tiro = proj.instance()
get_parent().add_child(tiro)
tiro.position = $Player_2d/ColorRect/Position2D.global_position
func stop():
if position.x <= 20:
position.x = 20
elif position.x >= 1280:
position.x = 1280
if position.y <= 0:
position.y = 0
elif position.y >= 720:
position.y = 720
First of all your ticks are irregular because frames can take longer or shorter, so you have to use delta.
Secondly, you’re using just_pressed in combination with your tick that only occurs once every 30 frames. So if you don’t press the button in the exact moment this one frame occurs, you don’t shoot anything.
I assume you wanted to shoot a bullet every 30 frames instead of only if the interval and the moment of pressing the keys match.
extends Node2D
var speed : float = 7
var up := false
var down := false
var left := false
var right := false
var proj := preload('res://Scenes/Ball.tscn')
var tick : float = 0
var tiro : bool = true
func time(delta : float) -> void:
tick += delta
if tick > 0.5: # time in seconds
tick -= 0.5
tiro = true
else:
tiro = false
func _process(delta : float) -> void:
move()
stop()
time(delta)
func move() -> void:
if Input.is_action_pressed("ui_right"):
position.x += speed
elif Input.is_action_pressed("ui_left"):
position.x -= speed
if Input.is_action_pressed("ui_up"):
position.y -= speed
elif Input.is_action_pressed("ui_down"):
position.y += speed
if Input.is_action_just_pressed("ui_select"):
# force shooting to start the moment you press down the key
tick = 0
tiro = true
if Input.is_action_pressed("ui_select") and tiro == true:
# shoot every frame the interval occurs and the key is held down
var tiro = proj.instance()
get_parent().add_child(tiro)
tiro.position = $Player_2d/ColorRect/Position2D.global_position