Godot Version
v4.6.stable.official
Question
Hi! I'm following the Kids can Code tutorial to build my very first space shooter! I'm up to the stage where I'm adding the bullet's motion, and I've got it shooting fine when you press the space bar, but the problem is that it shoots once and then doesn't reload. I suspect an issue with the cooldown loop, but I can't figure out it. Pasting the player script first, followed by the bullet script.
extends Area2D
@onready var screensize = get_viewport_rect().size
@export var speed = 150 #makes the speed visible in the inspector; sets default to 150
@export var cooldown : float
@export var bullet_scene : PackedScene
var can_shoot = true
func _ready():
start()
func start ():
position = Vector2(screensize.x / 2, screensize.y -64)
$GunCooldown.wait_time = cooldown
func shoot():
if not can_shoot:
return
can_shoot = false
$GunCooldown.start()
var b = bullet_scene.instantiate()
get_tree().root.add_child(b)
b.start(position + Vector2(0, -8))
func _process(delta):
var input = Input.get_vector("left", "right","up","down")
if input.x >0:
$Ship.frame = 2
$Ship/Boosters.animation = "Right"
elif input.x <0:
$Ship.frame = 0
$Ship/Boosters.animation = "Left"
else:
$Ship.frame = 1
$Ship/Boosters.animation = "Forward"
position += input * speed * delta
position = position.clamp(Vector2(8,8), screensize - Vector2(8,8))
if Input.is_action_pressed("shoot"):
shoot()
func _on_gun_cooldown_timeout():
can_shoot = true
extends Area2D
@export var speed = -250
func start(pos):
position = pos
func _process(delta):
position.y += speed * delta
func _on_area_entered(area):
if area.is_in_group("enemies"):
area.explode()
queue_free()
func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()