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

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
Godot version v3.3.2 stable official
in Engine by (26 points)

1 Answer

0 votes

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
by (28 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.