Ok imagine the context ! I'm trying to create a mario party game and one of the mini games involves your character being attacked by cats named kapu kapu. They come from different points of the maps and if you touch one of them, the mini game is over.
Except that whenever i try to make another spawn after the previous one disappeared, the script keeps running but no other cat appears which you guess, makes the mini game ridiculously easy.
So here are the two scripts, the first one refers to my mini game while the second one refers to the enemmy who possesses a defined hitbox along with sprites.
`extends Node2D
var game_over : bool = false
func _ready() -> void:
$"Beach Music".play()
# CRÉATION AUTOMATIQUE DU SPAWNER EN CODE (100% GARANTI)
var spawn_timer = Timer.new()
spawn_timer.wait_time = 1.5
spawn_timer.one_shot = false
spawn_timer.autostart = true
spawn_timer.timeout.connect(_on_spawn_timer_timeout)
add_child(spawn_timer)
# Gestion du chrono global de 30 secondes
if has_node("GameTimer"):
$GameTimer.timeout.connect(_on_game_timer_timeout)
$GameTimer.wait_time = 30.0
$GameTimer.one_shot = true
$GameTimer.start()
func _process(delta: float) -> void:
if not game_over:
if has_node("GameTimer") and has_node("Timer"):
$Timer.text = str(ceil($GameTimer.time_left))
elif has_node("GameTimer") and has_node("TimerLabel"):
$TimerLabel.text = str(ceil($GameTimer.time_left))
func _on_spawn_timer_timeout() -> void:
if game_over:
return
var kapukapu_scene = load("res://kapu.tscn")
var enemy = kapukapu_scene.instantiate()
var side = randi() % 2
var random_y = randf_range(200.0, 500.0)
if side == 0:
enemy.position = Vector2(-50.0, random_y)
enemy.direction = Vector2.RIGHT
else:
enemy.position = Vector2(1200.0, random_y)
enemy.direction = Vector2.LEFT
enemy.speed = randf_range(200.0, 320.0)
add_child(enemy)
print("Timeout spawn, game_over = ", game_over)
if game_over:
return
func end_game_hit(player_name: String) -> void:
print("end_game_hit appelé par ", player_name)
if game_over:
return
game_over = true
if has_node("GameTimer"):
$GameTimer.stop()
if has_node("Timer"):
$Timer.text = "GAME OVER!"
elif has_node("TimerLabel"):
$TimerLabel.text = "GAME OVER!"
print("GAME OVER ! ", player_name, " S'EST FAIT CROQUER !")
await get_tree().create_timer(3.0).timeout
get_tree().change_scene_to_file("res://menu.tscn")
func _on_game_timer_timeout() -> void:
if game_over:
return
game_over = true
if has_node("Timer"):
$Timer.text = "TIME'S UP!"
elif has_node("TimerLabel"):
$TimerLabel.text = "TIME'S UP!"
print("VICTOIRE ! SURVÉCU AUX 30 SECONDES !")
await get_tree().create_timer(3.0).timeout
get_tree().change_scene_to_file("res://menu.tscn")`
`extends Area2D
@export var speed : float = 250.0
var direction : Vector2 = Vector2.LEFT
func _process(delta: float) -> void:
position += direction * speed * delta
$AnimatedSprite2D.flip_h = (direction.x > 0)
if position.x < -200 or position.x > 1400:
queue_free()
func _on_body_entered(body: Node2D) -> void:
if body is CharacterBody2D:
get_parent().end_game_hit(body.name)
`