Godot 3.2 Guide To Build Your 2D Platformer Game – Part 2 (Creating a Platform)

This is the second part on how to build a 2D Platformer Game using Godot game engine. If you miss the first part, here is the link for it Godot 3.2 Guide To Build Your 2D Platformer Game – Part 1

Advertisements

In this part we will build our platforms where the user can jump to.

In the FileSystem, right click to the Scenes folder then add a new Folder and name it Platforms

Right click to Platforms folder and click New Scene

Then a new scene will be created. In the scene panel click on the Other Node.

Then search for KinematicBody2D then click Create.

Now you will have this.

Right click to the KinematicBody2D and rename it to GroundGrass then save it. Then Right click to the GroundGrass to Add Child Node

Search for the sprite, select it then click Create

Click on the Sprite node, then look for the ground_grass.png in the Assets folder. Then drag it to the sprite texture

If you did it right, then you should have this

Advertisements

Right click again in the GroundGrass then add child node. Search for CollisionPolygon2D then click Create.

Then draw a polygon around the sprite. You can do it by clicking on the edge of the sprite. Each click will create a point and there will be a line that will connect the points. Continue doing it until you manage to surround the sprite. Then end the polygon by clicking on the first point.

Advertisements

We need to add a script to our GroundGrass platform so we can know the width of the sprite. Then later we will use it to calculate the spawn location of our next platform. To do that, click on the GroundGrass, then click on the paper icon at the top of the GroundGrass Scene.

Then click this icon to browse for path of our script.

Click on this up icon to go out of Platforms folder, then click one more time go to res:// folder.

Then click the Create Folder button then add a new folder and name it Scripts.

Rename the file to Platform.gd. We will add the same script later to our other platform. Then click Open

Then click Create

Now update the code of our Platform.gd to look like this

Or you can just copy this code.

extends KinematicBody2D

var sprite_width

func _ready():
	sprite_width = $Sprite.texture.get_size().x * scale.x
Advertisements

What we did in that code, is we declare a variable called sprite_width then calculate the width of the sprite in the function ready and assigned it to variable sprite_width.

Later we will create our platforms dynamically, so we need some kind of trigger to know when we will create a new platform. To do that let’s create our custom Signals.

Right click to the Scripts folder then click New Script

Let’s name the new script as Signals.gd

Then update the Signals.gd code to this

Or copy this code to Signals.gd

extends Node

signal create_new_platform

Then right click to the GroundGrass then add child node. Search for VisibilityNotifier2D then click Create

Click the VisibilityNotifier2D then in the right side of the window click the Node tab

Double click to the screen_entered() then a pop up window will appear. Click Connect

It will add this function in the Platform.gd script

Update your Platform.gd script to this

Or copy this code to Platform.gd
extends KinematicBody2D

var sprite_width

func _ready():
	sprite_width = $Sprite.texture.get_size().x * scale.x



func _on_VisibilityNotifier2D_screen_entered():
	Signals.emit_signal("create_new_platform")
Advertisements

Notice, the line 11 has a red highlight on it? It said the identifier signals isn’t declared in the current scope. To resolve it, we need to make our Signals.gd a singleton or something that will load when our project loaded. To do that click on the Project Menu then click on Project Settings

Click the AutoLoad tab, then click the folder icon.

Click on Scripts

Click Signals.gd then click Open

Click Add

Then click close

It should now resolve the error. If you still got the error and you know you follow my instructions. Then you can right click to the Platform.gd then click close.

Advertisements

Then open it again by going to Scripts folder then double click to Platform.gd

Let’s add one more platform. We will follow the same steps that we did earlier to create our GroundGrass scene. Right click to Platforms folder then New Scene. Name the scene as GroundGrassSmall then click on the Other Node and search for KinematicBody2D.

Rename the the KinematicBody2D to GroundGrassSmall. Add Sprite to it then use the image ground_grass_small.png for the texture.

Then add a CollisionPolygon2D and draw a polygon around the sprite.

Click the GroundGrassSmall then click the paper icon to add a script to it.

Click the folder icon

Then navigate to scripts folder. Select the Platform.gd script then click Open.

Then click Load button

We still need to add the VisibilityNotifier2D to our GroundGrassSmall scene. Right click to GroundGrassSmall then add child node. Search for VisibilityNotifier2D then click Create.

Same as earlier click the VisibilityNotifier2D then click the Node tab at the right side of the window. Then double click to screen_entered(). A window will pop up then click the Connect button.

It should bring you to Platform.gd script. Then we are done adding our new platform.

If you want to add more platform, you can follow the same steps.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s