This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Bomb explosion doesn't affect player or npc

2dvoxel2dvoxel Posts: 7Member
edited June 2020 in Programming

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 Area2D

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

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

func _on_RemoveTimer_timeout():

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)

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

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

Note that the Bomb instances are managed by group called bombs, not sure if this changes anything.

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?


  • MegalomaniakMegalomaniak Posts: 2,580Admin

    Edited the OP to include code formatting.

  • cyberealitycybereality Posts: 928Moderator

    I think the code that is working is because they are physics bodies. You could make the bomb a physics body (like KinematicBody2D) and I think that would fix it.

  • 2dvoxel2dvoxel Posts: 7Member

    The player is an area2d, the mob is rigidBody2d and the bomb is an area2D object, i actually played with rigidBody2d , but i got the same results

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin

    Have you connected the signals? Maybe they are not firing because the signals are not connected or are connected to different functions.

    Also, you may want to handle the bomb colliding with other entities in the bomb itself, since it is an Area2D node, which can essentially be thought of as a trigger in this case. When I have used Area2D nodes, I generally have the Area2D's code handle collision detection since then I can use the body_entered/body_exited and area_entered/area_exited signals to detect collisions with the Area2D.

  • 2dvoxel2dvoxel Posts: 7Member

    So i finally figured this out, here is how i solved this in case someone else runs into something similar.

    I changed the type of root node for the Bomb scene, i made it a StaticBody2D with this change the bombs and mobs can interact.

    For the mobs to be detecting bombs collision added a Area2d with a child CollisionShape2D, so it can interact with the player and the mobs.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.