![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | AllenOwlferd |
I’m new to this engine (started around 2 months ago). I have an assignment related to this.
Here’s my problem: I have a sword with a Area2D main node and CollisionShape2D. The node has a “on_body_entered” signal connected to itself (Area2D node), which contains codes to check if the body is_in_group(“enemy”). If so, it will play another method called “dead()” under the enemy script. However, it isn’t doing exactly that. To test out whether the method dead() is called or not, I added “velocity = Vector2(0, 0)” under the method to check whether the enemy will stop moving if the sword has hit the target. Unfortunately, it didn’t.
Sword.gd
extends Area2D
var is_attacking = false
func _physics_process(delta):
pass
func attack():
if is_attacking == false:
is_attacking = true
$AnimatedSprite.play("attack")
func _on_AnimatedSprite_animation_finished():
is_attacking = false
$AnimatedSprite.stop()
func _on_Sword_body_entered(body):
if body.is_in_group("enemy"):
body.dead()
Enemy.gd
extends KinematicBody2D
var speed = 250
var velocity = Vector2()
var player
var dead = false
func dead():
dead = true
velocity = Vector2(0, 0)
queue_free()
func _physics_process(delta):
if dead == false:
velocity = Vector2()
if player:
velocity = (player.position - position).normalized() * speed
velocity = move_and_slide(velocity)
func _on_DetectRadius_body_entered(body):
if body.is_in_group("player"):
player = body
func _on_DetectRadius_body_exited(body):
player = null
Here’s a link to the said project folder.
I apologise beforehand for the mess that is in the project folder. I appreciate any help given, but at the same time, I’ll keep looking for solutions. Thank you in advance.
EDIT: This question has been solved, however I will leave the folder here with the mistake. I hope anyone who encounters a similar problem as I did can check the answer and the project folder to solve it. If not, I wish you good luck in finding the answer!
Rather than hacking a velocity change, did you try using breakpoints or prints to see if all the methods you expect being called are actually called? It could be collision isn’t detected, or the enemy isn’t in the enemy
group (could be a typo).
Zylann | 2019-09-06 17:35
Yes, I have tried using a print, however that didn’t work as well. It might’ve been the collision not being detected, because I have double confirmed that the enemy group isn’t a typo and is indeed assigned to the enemy scenes. My question currently is why the collision isn’t detected.
AllenOwlferd | 2019-09-06 17:42