Move between levels

In this part, I will show you how to be able to move between levels 1,2,3,etc when reaching the end of a level.

Create New Scene

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levelsFirst, you create a new Scene with Area2D as the root node.

Here, I use Area2D because I will create an area and when the PLAyer enters it will move to the next level.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 97

Done, then save it in the Scene folder.[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 98

Next, add the script to it.

The script includes the following:

extends Area2D


export (String,FILE) var LevelKeTiep


func _on_KhuVucDiChuyen_body_entered(body):
    if body.name == "Player":
        get_tree().change_scene(LevelKeTiep)

I declare a variable and this variable is special where I use it export(this is the command that will make the variable appear outside the Inspector so it can be easily edited)

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 99I add (String,FILE) behind to be able to select files from outside the Inspector.

More documentation can be found here: GDScript exports

As for the function below, you have to connect signal to appear.

if body.name == “Player” is that I check that if the Body goes in and the name is Player (name of the character scene that you and I have created), it will move to the Level assigned to the above variable.

[Tạo 2D Platformer Game với Godot]  Part 9: Move between levels 100

You click on Area2D

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 101

Through the Node tab, click 2 and click on the body enter (here I used the body entered because the Player is a KinematicBody2D, so I will use the signal to determine when a body enters the Area2D)

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 102

The table that appears, select Area2D and then click Connect.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 103[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 104

You can tell that the signal has been connected successfully by looking at the two symbols in the two pictures above.

New Level

After you’re done, create yourself a new level similar to how you create a Scene Map.[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 105

After creating a new 2D node, rename it to map2.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 106

And instead of redoing the tilemap, you can go to the first map to copy it.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 107

Go to map 2 and paste it out.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 108

After pasting, we get the same tilemap as the first Map and you just need to adjust it[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 109

My Map 2 after editing.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 110

Add Player and Background like Map 1, copy it and you’re done.

Instance teleportation area

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 111

Your Instance node has just moved to Map1 [Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 112

Right-click and select editable children to edit it.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 113

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 114

Select CollisionShape2D and select its shape.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 115

After selecting the shape, it will appear like this.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 116

And you drag it to the place where we need to change the level by clicking on KhuVucDi Chuyen and clicking on the move icon on the toolbar, but don’t drag the CollisionShape2D away.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 117

I have moved to the position I want and I edited the CollisionShape2D so that it is reasonable.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 118

You won’t be able to move yet because you haven’t told it which scene you want to move through so in KhuVucDichChuyen click on the folder icon.[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 119[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 120
[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 121

Find and select Map2 then click Open.

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 122

Once done, Map2 has been added to the LevelKeTiep variable[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 123

[Tạo 2D Platformer Game với Godot]  Part 9: Moving between levels 124

Above, when I enter, I will be transferred to Map2.

Here, you can apply many ways to make the most of it, such as:

  • Set camera limit in front of the moving area
  • Add Sprite in Scene KhuVucDiChuyen for easy identification.

summary

In this section, I showed you how to move between levels together and depending on your creativity and understanding, you can design many other things.

Leave a Reply