In this part, I will show you how to collect coins like other platformer games with Godot.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
Art
Download
As usual, I will provide the art.
You can download it at this link: Rotating Coins
Setting
You create a new folder named VatPham in Folder Assets and then copy the Fill Coins.png in the zip file downloaded above.
Or if you already have enough knowledge, you can use another image.
Coins
Add scene
You create a new scene and Area2D is the root node -> rename to DongXu then you add 2 nodes like the picture.
Here, I use Area2D because I want when the Player touches it, the coin will identify and disappear, not using other nodes to do anything confusing.
Then you add a new animation and drag the image in order 1- > 8 ( and remove the number 5 ).
Enable playing in animatedsprite so that it always runs the animation.
Then add collisionshape2d to it.
Then save the scene and add a script to it.
Then adjust the scale like the picture because it is quite big.
Script
You connect the signal is body_entered because I want to check if the player goes in, the coin disappears and the number of coins in the player increases.
extends Area2D func _on_DongXu_body_entered(body): if body.name == "Player": body.dongxu += 1 queue_free() pass # Replace with function body
Therefore, I will have a code like this in the script of the coin.
Player
Script
var dongxu = 0
And in the player’s script, I just need to add 1 more line that is var dongxu = 0.
func _process(delta): $GUI/DongXu.text = str("Dong xu:" + dongxu)
Then I added one more function, _process(), I use it to run non-physics problems, this function will process faster than physics process.
$GUI/DongXu.text = str(dongxu)
is that I will assign the text of node DongXu = with the value of the variable dongxu.
Note: when assigning a certain value to the text of a certain node, you always have to leave it in str() or else you will get an error because if you call .text, only String will be accepted.
Scene
Next, I have to add one more so-called to display the number of coins outside the screen for the player to know.
Add yourself a CanvasLayer -> GUI
One Label node -> DongXu
At the node label there is a so called text table and you can type there
Then I adjusted the angle to make it reasonable.
Map
Then I will go out of the map to Instance.
Here is my final result.
And when you pick up the coin, it will show up above the Label.
Summary
That’s it, see you in the next post.