1
\$\begingroup\$

I finished the Godot 2D game tutorial Dodge the Creeps, and i'm adding a few additional features to reinforce the things learned and make more interesting the base game.

Basically i added a Scene with a area2d as root node with a collisionShape2d,a 2 timers for the Bomb scene.

The bomb appears randomlly in the main scene, for a few seconds is supposed to be ticking and then explodes.

My problem is that neither the player or the creeps( the npc enemies) are being removed from the scene. They don't interact at all,

This is the code for the Bomb.tscn scene:

extends Node

func _ready():
    # Area collison disabled until animation finishes
    $CollisionShape2D.disabled = true
    # Bomb ticking animation
    $AnimatedSprite.animation = "bomb"
    # Until BombTimer times out we change animation and activate collisionArea
    $BombTimer.start()

func _on_BombTimer_timeout():
    # Change animation sprite
    $AnimatedSprite.animation = "explosion"
    # Enable Collision area
    $CollisionShape2D.disabled = false
    # Start timer to remove bomb object
    $RemoveTimer.start()

func _on_RemoveTimer_timeout():
    queue_free()

The code for the creep/mob when hit by the bomb:

func _on_Mob_body_shape_entered(body_id, body, body_shape, local_shape):
     $CollisionShape2D.set_deferred("disabled", true)
     queue_free()

The code for collision detection for the player when is hit:

func _on_Player_body_entered(body):
     hide()  # Player disappears after being hit
     emit_signal("hit")
     $CollisionShape2D.set_deferred("disabled", true)

If the creep/mob touches the player the game is over, but if the bombs explodes nothing happens. What i'm missing in the code?

\$\endgroup\$
3
+50
\$\begingroup\$

The body_shape_entered signal doesn't detect Areas, only Bodies (Kinematic, Rigid, or Static). In general Bodies can't detect Areas.

However, Areas can detect Bodies. So you can use the body_shape_entered signal on the Bomb Node. Then you can check the type of body collided with, and call a method on the Mob.

On Bomb.gd after connecting the signal:

var Mob = preload("Mob.gd")
func _on_bomb_body_shape_entered(body_id, body, body_shape, area_shape):
    if body is Mob:
        body.die()
        queue_free()

On Mob.gd:

func die():
    queue_free()

Since the player is an Area, we can use the Area's area_entered signal similarly.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.