[Tạo 2D Platformer Game với Godot] Part 16: Collecting Coins

In this part I will show you how to collect coins like other platformer games with Godot.

Art

Download

As usual, I will provide the art.

You can download it at this link: Rotating Coin

Setting

[Tạo 2D Platformer Game với Godot]  Part 16: Collecting Coins

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 61 . Coins

You create a new scene and Area2D is the root node -> rename it to DongXu then you add 2 nodes as shown.

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 62 . Coins

Then you add a new animation and drag the image in the order 1- > 8 ( and remove the number 5 ).

[Tạo 2D Platformer Game với Godot]  Part 16: Collect 63 . Coins

Enable playing in animatedsprite so that it always runs the animation.

[Tạo 2D Platformer Game với Godot]  Part 16: Collect 64 . Coins

Then add collisionshape2d to it.

[Tạo 2D Platformer Game với Godot]  Part 16: Collect 65 . Coins

Then save the scene and add a script to it.

[Tạo 2D Platformer Game với Godot]  Part 16: Collect 66 . Coins

Then adjust the scale like the picture because it is quite big.

Script

[Tạo 2D Platformer Game với Godot]  Part 16: Collect 67 . Coins[Tạo 2D Platformer Game với Godot]  Part 16: Collect 68 . coin

You connect the signal as 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 69 . Coin

Add yourself 1 CanvasLayer -> GUI

A Label node -> DongXu
[Tạo 2D Platformer Game với Godot]  Part 16: Collect 70 . Coins

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 71 . Coins[Tạo 2D Platformer Game với Godot]  Part 16: Collect 72 . Coins

Then I adjusted the angle to make it reasonable.

Map

[Tạo 2D Platformer Game với Godot]  Part 16: Collect 73 . Coin

Then I will go out of the map to Instance.

[Tạo 2D Platformer Game với Godot]  Part 16: Collect 74 . Coin

Here is my final result.

[Tạo 2D Platformer Game với Godot]  Part 16: Collect 75 . Coins[Tạo 2D Platformer Game với Godot]  Part 16: Collect 76 . Coins

And when you pick up the coin, it will show up above the Label.

Summary

That’s it, see you in the next post.

Leave a Reply