[Tạo 2D Platformer Game với Godot] Part 23: Choosing Level – LLODO


In this part, I will guide you to create a button to select a level like in the Map level there are some level 1,2,3 cells and then choose to play.

Select Level

Scene[Tạo 2D Platformer Game với Godot]  Part 23: Choosing Level

You create yourself a new scene with Control as the root node and 1 TextureButton and 3 Texturects.

TextureButton is what I use to create an image for the level and easily check if clicked.

The 3 Texture Rects are used to display the star.

[Tạo 2D Platformer Game với Godot]  Part 23: Choosing Level 70

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 71

You copy the circle on the side of the UI folder in the Level map to the TextureButton
[Tạo 2D Platformer Game với Godot]  Part 23: Choosing Level 72

[Tạo 2D Platformer Game với Godot]  Part 23: Choosing Level 73

Then the Texture Rect then 3 drag the star in.

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 74

Then adjust the stars in order from left to right.

[Tạo 2D Platformer Game với Godot]  Part 23: Choosing Level 75

Then I renamed it again.

[Tạo 2D Platformer Game với Godot]  Part 23: Choosing Level 76

Add a Label named SoLevel to display Level.

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 77

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 78

I used Customs Font in the previous section.

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 79

Then save the scenes again.

[Tạo 2D Platformer Game với Godot]  Part 23: Choosing Level 80

Then add it the Script.

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 81

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 82

Then connect the signal pressed of HinhTron.

Script

extends Control

export (String) var MapName
export (String,FILE) var Scene
export (String) var Level
onready var ngoisaovang = preload("res://Assets/LevelMap/3 UI/Star1.png")
var SoDiem = 0

func _ready():
    $SoLevel.text = Level
func _process(delta):
    if SoDiem >= 1 and SoDiem <= 30:
        $NgoiSao1.texture = ngoisaovang
    elif SoDiem >= 30 and SoDiem <= 95:
        $NgoiSao1.texture = ngoisaovang
        $NgoiSao2.texture = ngoisaovang
    elif SoDiem >= 95:
        $NgoiSao1.texture = ngoisaovang
        $NgoiSao2.texture = ngoisaovang
        $NgoiSao3.texture = ngoisaovang


func _on_HinhTron_pressed():
    get_tree().change_scene(Scene)
    pass # Replace with function body.

As for the code, I will have the above.

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 83

Here I will use export to export 3 variables to Inspector

And Sodiem here I will follow the 100 scale. SoDiem here will be calculated from the number of stars that you collect when playing 1 level if all stars are enough, SoDiem is 100 and $NgoiSao1,2,3 will switch to yellow.

line has onready var I use it to load prior to The image of the yellow star means that when you run the game, it will always load, but if you use load(), it will load the image when you touch it.

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 84

Then Instance the newly created Level into the LevelMap.

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 85

[Tạo 2D Platformer Game với Godot]  Part 23: Choosing Level 86

You fill in the information.

MapName is the name of the root node of the underlying Scene.

Scene is you choose the scene of that level.

Level is used to display the level.

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 87

MapName with the original node name.

[Tạo 2D Platformer Game với Godot]  Part 23: Choose Level 88

And after running the game, I will have the results as above.

After clicking on it, I will be transferred to Level 1.

summary

In the next section, I will show you how to create a stop menu and when you win a level.



Link Hoc va de thi 2021

Chuyển đến thanh công cụ