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
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 22 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collecting Coins](https://anonyviet.com/wp-content/uploads/2021/11/15-11-2021-11-57-51.png)
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
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 23 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 19 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/15-11-2021-11-50-37.png)
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.
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 24 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 20 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/15-11-2021-11-57-14.png)
Then you add a new animation and drag the image in order 1- > 8 ( and remove the number 5 ).
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 25 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 21 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-05-02.png)
Enable playing in animatedsprite so that it always runs the animation.
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 26 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collecting 22 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-03-23.png)
Then add collisionshape2d to it.
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 27 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 23 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-02-48.png)
Then save the scene and add a script to it.
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 28 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 24 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-19-37.png)
Then adjust the scale like the picture because it is quite big.
Script
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 30 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 26 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-04-27.png)
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.
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 31 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 27 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-12-30.png)
Add yourself a CanvasLayer -> GUI
One Label node -> DongXu![[Tạo 2D Platformer Game với Godot] Part 16: Collect 32 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 28 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-12-37.png)
At the node label there is a so called text table and you can type there
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 34 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 30 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-13-00.png)
Then I adjusted the angle to make it reasonable.
Map
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 35 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 31 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-20-22.png)
Then I will go out of the map to Instance.
![[Tạo 2D Platformer Game với Godot] Part 16: Collecting Coins [Tạo 2D Platformer Game với Godot] Part 16: Collecting Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-20-52.png)
Here is my final result.
![[Tạo 2D Platformer Game với Godot] Part 16: Collect 38 . Coins [Tạo 2D Platformer Game với Godot] Part 16: Collect 34 . Coins](https://anonyviet.com/wp-content/uploads/2021/11/16-11-2021-12-21-47.png)
And when you pick up the coin, it will show up above the Label.
Summary
That’s it, see you in the next post.









