Hey, I've been trying to get this piece of code to work. I am trying to make a game where you can kill an enemy with your mouse click. I have tried it by using signals but i am not sure i am doing it right. Here is my code:
From my main scene:
extends Node
func _ready():
pass
var enemy = preload ("res://enemy.tscn")
func _on_timer_timeout():
var new_spawn = enemy.instantiate()
var rng = RandomNumberGenerator.new()
var randomx = rng.randf_range(0.0, 1152.0)
var randomy= rng.randf_range(50.0, 600.0)
new_spawn.position = Vector2(randomx, randomy)
add_child(new_spawn)
print("spawn")
func _on_enemy_hit():
if is_instance_valid(enemy):
enemy.queue_free()
print("delete")
from my enemy scene:
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta
move_and_slide()
var mouse_hit
func _on_mouse_entered():
mouse_hit = true
func _on_mouse_exited():
mouse_hit = false
var raak
signal hit
func _input(event):
if mouse_hit and Input.is_action_pressed("mouse_left"):
print("raak")
hit.emit()
Can somebody help me out