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.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
Create New Scene
First, 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.
Done, then save it in the Scene folder.
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)
I add (String,FILE) behind to be able to select files from outside the Inspector.
You can find more documentation 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 comes in and the name is Player (name of the character scene that you and I created), it will move to the Level assigned to the above variable.
You click on Area2D
Through the Node tab, click 2 and click on the enter body (here I use the body entered because the Player is a KinematicBody2D, so I will use the signal to determine when a body enters Area2D)
The table that appears, select Area2D and then click Connect.
You can know 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.
After creating a new 2D node, rename it to map2.
And instead of redoing the tilemap you can go to the first map to copy it.
Go to map 2 and paste it out.
After pasting, we get the same tilemap as the first Map and you just need to adjust it
My Map 2 after editing.
Add Player and Background like Map 1, copy it and you’re done.
Instance teleportation area
You Instance node has just moved to Map1
Right-click and select editable children to edit it.
Select CollisionShape2D and select its shape.
After selecting the shape, it will appear like this.
And you drag it to the place where we need to change the level by clicking on the Area and clicking the move icon on the toolbar, but don’t drag the CollisionShape2D away.
I have moved to the position I want and I edited the CollisionShape2D so that it is reasonable.
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.
Find and select Map2 then click Open.
Once done, Map2 has been added to the LevelKeTiep variable
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 have shown you how to move between levels together and depending on your creativity and understanding, you can design many other things.