[Tạo 2D Platformer Game với Godot] Part 18: Using the buttons – LLODO


In this part, I will show you how to use buttons like start and exit the game by connecting a signal and a few lines of code.

Use

Add scripts

[Tạo 2D Platformer Game với Godot]  Part 18: Using the buttons

[Tạo 2D Platformer Game với Godot]  Part 18: Using 46 . buttons

[Tạo 2D Platformer Game với Godot]  Part 18: Using 47 . buttons

You add yourself a new script in the scene Menu.

Signal connection
[Tạo 2D Platformer Game với Godot]  Part 18: Using the 48 . buttons

I will connect the signal of the NutBatDau node so that when I click it, it will move to scene level 1

[Tạo 2D Platformer Game với Godot]  Part 18: Using the 49 . buttons

Find and connect the pressed() signal. This signal will execute the command when the button is clicked by the user.

[Tạo 2D Platformer Game với Godot]  Part 18: Using the 50 . buttons

Click connect
[Tạo 2D Platformer Game với Godot]  Part 18: Using the 51 . buttons

Connect successfully.

[Tạo 2D Platformer Game với Godot]  Part 18: Using the 52 . buttons

[Tạo 2D Platformer Game với Godot]  Part 18: Using the 53 . buttons [Tạo 2D Platformer Game với Godot]  Part 18: Using the 54 . buttons [Tạo 2D Platformer Game với Godot]  Part 18: Using 55 . buttons

You will do the same for yourself for the game exit button.

Code

extends Control




func _on_NutBatDau_pressed():
    pass # Replace with function body.


func _on_NutThoatGame_pressed():
    pass # Replace with function body.

Here is the original code.

extends Control




func _on_NutBatDau_pressed():
    get_tree().change_scene("res://Scences/Map/Map.tscn")
    pass # Replace with function body.


func _on_NutThoatGame_pressed():
    get_tree().quit()
    pass # Replace with function body.

And here is the code that I added and it only adds 2 lines.

At the start button I will add the function change_sence() to change through the scene I want and change_scene() can only be called through get_tree().

At the game exit button, I use the quit() command, this command will automatically close your game

[Tạo 2D Platformer Game với Godot]  Part 18: Using 56 . buttons

Then you press F6 run the game and will see that when you press BAT DAU you will be moved to another scene and when you press THOAT GAME your game will be exited.

summary

This part is full of things that are quite easy to do.

In the next part, I will guide you how to design and redecorate the Menu interface so that it is beautiful and reasonable. It is expected that the Menu section will last for 2.3 more parts and then create a Menu to stop the game while playing the game.



Link Hoc va de thi 2021

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