I'm working on my first project, I've looked into a bunch of tutorials to figure out what's wrong with the code or maybe a different method like layering but I cant seem to figure out how to do enemy collision. i would really appreciate some help
maingd.
extends Node2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
`_setup_level()`
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
`pass`
func _setup_level() -> void:
`#connect enemies`
`var enemies = $levelroot.get_node_or_null("Enemies")`
`if enemies:`
`for enemy in enemies.get_children():`
`enemy.player_died.connect(_on_player_died)`
# - - - - - - - - -
# signal handlers
#- - - - - - - - -
func _on_player_died(body):
`print(body)`
`print("Player_killed")`
.................................................................................................................
playergd.
extends CharacterBody2D
u/onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
u/onready var jump_sound: AudioStreamPlayer2D = $"jump sound"
const SPEED = 300.0
const JUMP_VELOCITY = -850.0
var alive = true
func _physics_process(delta: float) -> void:
`if !alive:`
`return`
`#Add animation`
`if velocity.x > 1 or velocity.x < -1:`
`animated_sprite_2d.animation = "walk"`
`else:`
`animated_sprite_2d.animation = "idle"`
`# Add the gravity.`
`if not is_on_floor():`
`velocity += get_gravity() * delta`
`animated_sprite_2d.animation = "jump"`
`# Handle jump.`
`if Input.is_action_just_pressed("jump") and is_on_floor():`
`velocity.y = JUMP_VELOCITY`
`jump_sound.play()`
`var direction := Input.get_axis("left", "right")`
`if direction:`
`velocity.x = direction * SPEED`
`else:`
`velocity.x = move_toward(velocity.x, 0, SPEED)`
`move_and_slide()`
`if direction == 1.0:`
`animated_sprite_2d.flip_h = false`
`elif direction == -1.0:`
`animated_sprite_2d.flip_h = true`
func die() -> void:
`AnimatedSprite2D.animation = "dying"`
`alive = false`
..............................................................................................
enemygd.
extends Area2D
u/onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
signal player_died
const SPEED = 100.0
var direction = 1
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
`pass # Replace with function body.`
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
`position.x += direction * SPEED * delta`
func _on_timer_timeout() -> void:
`direction *= -1`
`animated_sprite_2d.flip_h = !animated_sprite_2d.flip_h`
func _on_body_entered(body: Node2D) -> void:
if body.name == "Player" and body.alive:
emit_signal("player_died", body)