Learn Godot 4 by Making a 2D Platformer — Part 7: Bomb Setup

christinec_dev

christine

Posted on July 24, 2023

Learn Godot 4 by Making a 2D Platformer — Part 7: Bomb Setup

*You can find the links to the previous parts at the bottom of this tutorial.

Previously we left off where we created our game’s first two levels. In this part, we will create our bomb that will visually travel across our scene when the bomb spawner spawns it. We’ll create the bomb spawner in the next part. You can think of the bomb as the Barrel in Donkey Kong, and the bomb spawner as the angry gorilla!


WHAT YOU WILL LEARN IN THIS PART:

  • How to work with StaticBody2D nodes.
  • How to remove nodes from scenes using queue_free().

Let’s create a new scene with an Area2D node as its root. It should have the following children: AnimatedSprite2D, CollisionShape2D (with a CircleShape2D assigned to it), and a Timer node. These nodes will allow us to check if the collision is colliding with our Player, and if true, it should play an animation and remove the bomb from our scene. If the bomb is not colliding with our player, it should “self-destruct” after a few seconds

Godot 2D Platformer

Let’s assign a new Script to our Scene and save it underneath your Scripts folder.

Godot 2D Platformer

Then, connect your Timer nodes timeout() signal to your script, as well as your Area2D nodes body_entered() signal.

Godot 2D Platformer

Godot 2D Platformer

Before we get coding, let’s set up a few animations. In our AnimatedSprite2D node, let’s create a new SpriteFrames resource with two animations: “moving” and “explode”.

Godot 2D Platformer

For your moving animation, navigate to “res://Assets/Kings and Pigs/Sprites/09-Bomb/Bomb On (52x56).png” and assign all four frames to your animation. You can leave the looping and FPS as the default values for now.

Godot 2D Platformer

For your explode animation, navigate to “res://Assets/Kings and Pigs/Sprites/09-Bomb/Boooooom (52x56).png” and assign all six frames to your animation. You can leave the looping and FPS as the default values for now.

Godot 2D Platformer

In your script, let’s make our moving animation our default animation when the Bomb enters the game’s scene.



    ### Bomb.gd 
    extends Area2D

    #default animation on spawn
    func _ready():
        $AnimatedSprite2D.play("moving")


Enter fullscreen mode Exit fullscreen mode

In our func _on_body_entered(body):, we need to check if the player is entering the collision shape of our Bomb scene, and if true, we should play our explode animation and start our Timer. The Timer will allow us to have an animation delay of 1 second, before removing our Bomb from our scene.



    ### Bomb.gd 

    extends Area2D

    #default animation on spawn
    func _ready():
        $AnimatedSprite2D.play("moving")

    func _on_body_entered(body):
        #if the bomb collides with the player, play the explosion animation and start the timer
        if body.name == "Player":
            $AnimatedSprite2D.play("explode")
            $Timer.start()


Enter fullscreen mode Exit fullscreen mode

Then, in our timeout() function, we need to remove the scene from our games scene tree. To remove a node from our scene, we use the object queue_free(). This is the most standardized way of removing or “destroying objects” from scene trees. There is also free(), the main difference between them is that queue_free() gets called the next frame. We’ll only remove our Bomb scene if the Bomb scene is valid (i.e., it exists).



    ### Bomb.gd 

    extends Area2D

    #older code

    #remove the bomb from the scene only if the Bomb exists
    func _on_timer_timeout():
        if is_instance_valid(self):
            self.queue_free()


Enter fullscreen mode Exit fullscreen mode

Now if you run your Main scene and you instance your Bomb, it should explode and remove itself from the scene if the Player runs into it.

Godot 2D Platformer

We also want the Bomb to explode once it reached the end of the path. We’ll set up this path in the next part, but to put it simply, this path will run from the bomb spawner’s position to the player’s start position. Once it reaches the end without colliding with the player, it should explode.

Godot 2D Platformer

We can do this via a new scene that will serve as our wall, or we can reference our “Level” Tilemap node as the body. This means if the Bomb collides with the tiles in our Level tilemap, it should explode. The downside to this is that you’ll have to be very careful when creating your path — since any collision with your Tilemap will explode the bomb. I’m going to show you both ways.

Option 1: Exploding the Bomb Using the Tilemap

To explode the bomb if it collides with our “Level” Tilemap node in our Main (and Main_2) scenes, we can simply extend our existing bomb functionality as follows:



    ### Bomb.gd 

    #older code

    func _on_body_entered(body):
        #if the bomb collides with the player, play the explosion animation and start the timer
        if body.name == "Player":
            $AnimatedSprite2D.play("explode")
            $Timer.start()

        #if the bomb collides with our Level Tilemap (floor and walls). 
        if body.name == "Level":
            $AnimatedSprite2D.play("explode")
            $Timer.start()


Enter fullscreen mode Exit fullscreen mode

Now anytime the bomb collides with a tile on your Level node, it should explode. Remember that the bomb will only explode if it collides with your walls or floors — any tile that you assigned collisions to. Your background and decorations should be safe. Move your bomb onto a tile to see if this works.

Godot 2D Platformer

Godot 2D Platformer

Option 2: Exploding the Bomb Using a Collision Scene

If you want your bomb to explode only when it reaches certain areas, you’ll have to create a new scene instead. Create a new scene with a StaticBody2D node as its root, and add a CollisionShape2D node to it. You can make the shape of the collision a RectangeShape2D and drag it down to be a long shape. A StaticBody2D node is a physics body for 2D physics that is static. It is useful for floors, walls, pickups, and in our case, collisions. Our Area2D node in our Bomb scene will be able to collide with the StaticBody2D node.

Godot 2D Platformer

Rename the root to “Wall” and save your Scene underneath your Scenes folder.

Godot 2D Platformer

Now, simply instance your Wall scene in your Main scene. You can change the x and y scale of the Wall via the Scale Mode tool. Place this Wall where you want your areas of the explosion to be. This way you can make the wall any size you want. For me, this will be the starting point of our scene.

Godot 2D Platformer

Godot 2D Platformer

Godot 2D Platformer

In our script, we can now check if the bomb is colliding with the Wall. We’ll use the begins_with method of the string to check if the name starts with “Wall”. Therefore, if we instance our Wall scene multiple times in our Main scene, it would explode even if the name is “Wall”, “Wall2”, “Wall_x”, etc.



    ### Bomb.gd 

    #older code

    func _on_body_entered(body):
        #if the bomb collides with the player, play the explosion animation and start the timer
        if body.name == "Player":
            $AnimatedSprite2D.play("explode")
            $Timer.start()

        #if the bomb collides with our Wall scene, explode and remove
        if body.name.begins_with("Wall"):
            $AnimatedSprite2D.play("explode")
            $Timer.start()


Enter fullscreen mode Exit fullscreen mode

Now if you place your Bomb in your Wall area and you run your scene, it should explode!

Godot 2D Platformer

If you have multiple Walls, you can organize them in a Node2D node.

Godot 2D Platformer

Your final code should look like this.

Congratulations on creating your bomb and bomb triggers! In the next part, we will create a bomb spawner that will spawn the bomb and move it along a certain path. It will continuously spawn each time the bomb explodes and is removed from the scene.

Now would be a good time to save your project and make a backup of your project so that you can revert to this part if any game-breaking errors occur. Go back and revise what you’ve learned before you continue with the series, and once you’re ready, I’ll see you in the next part!


Next Part to the Tutorial Series

The tutorial series has 24 chapters. I’ll be posting all of the chapters in sectional daily parts over the next couple of weeks. You can find the updated list of the tutorial links for all 24 parts of this series on my GitBook. If you don’t see a link added to a part yet, then that means that it hasn’t been posted yet. Also, if there are any future updates to the series, my GitBook would be the place where you can keep up-to-date with everything!

Godot 2D Platformer


Support the Series & Gain Early Access!

If you like this series and would like to support me, you could donate any amount to my KoFi shop or you could purchase the offline PDF that has the entire series in one on-the-go booklet!

The booklet gives you lifelong access to the full, offline version of the “Learn Godot 4 by Making a 2D Platformer” PDF booklet. This is a 451-page document that contains all the tutorials of this series in a sequenced format, plus you get dedicated help from me if you ever get stuck or need advice. This means you don’t have to wait for me to release the next part of the tutorial series on Dev.to or Medium. You can just move on and continue the tutorial at your own pace — anytime and anywhere!

Godot 2D Platformer

This book will be updated continuously to fix newly discovered bugs, or to fix compatibility issues with newer versions of Godot 4.

💖 💪 🙅 🚩
christinec_dev
christine

Posted on July 24, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related