Create a new Scene and call it Coin
Create a new Scene and call it Coin
Add an Area2D node as the root node.
Add an Area2D node as the root node.
This is a node that can detect collisions when a collider like our Player node walks over it.
This is a node that can detect collisions when a collider like our Player node walks over it.
Add a Sprite2D node as a child of Area2D
Add a Sprite2D node as a child of Area2D
Add the Coin.png file as the Sprite's texture.
Add the Coin.png file as the Sprite's texture.
Rename Area2D to Coin
Rename Area2D to Coin
Rename Sprite2D to Sprite
Rename Sprite2D to Sprite
Add a Collision2D Node as a child of Coin
Add a Collision2D Node as a child of Coin
Give it a Circle Shape
Give it a Circle Shape
Fit the shape around the coin
Fit the shape around the coin
COIN SCRIPT
COIN SCRIPT
Select the Coin (Area2D) and add a new script.
Select the Coin (Area2D) and add a new script.
The script will already have the default _ready and _process functions:
The script will already have the default _ready and _process functions:
Delete them:
Delete them:
SIGNALS
SIGNALS
Signals are events that are triggered when something happens in our game.
Signals are events that are triggered when something happens in our game.
We will be using the body_entered signal for our script.
We will be using the body_entered signal for our script.
Signals detect when an object has collided with Area2D
Signals detect when an object has collided with Area2D
To set this up:
To set this up:
make sure the Coin (Area2D) node is selected
make sure the Coin (Area2D) node is selected
move to the Node window in the Editor.
move to the Node window in the Editor.
Click on Signals
Click on Signals
Select body_entered Signal
Select body_entered Signal
Click on CONNECT
Click on CONNECT
Click on Connect again.
Click on Connect again.
This will generate an _on_body_entered function in the script,
This will generate an _on_body_entered function in the script,
and automatically connect it to the Area2D signal.
and automatically connect it to the Area2D signal.
Now we can customise the bahaviour of the function in our script.
Now we can customise the bahaviour of the function in our script.
Increase the scale of the body that has entered the area.
Increase the scale of the body that has entered the area.
Destroy the coin so it can’t be picked up a second time.
Destroy the coin so it can’t be picked up a second time.
Add some coins to the Player Scene.
Add some coins to the Player Scene.
Test the game
Test the game
Notice when the player grabs a coin the Player grows
Notice when the player grabs a coin the Player grows
And the coin is destroyed.
And the coin is destroyed.
We can add more coins to the Player scene
We can add more coins to the Player scene
Just by dragging and dropping the Coin Scene file
Just by dragging and dropping the Coin Scene file
From the FileSystem
From the FileSystem
Test again and watch the Player grow for each coin they collect!
Test again and watch the Player grow for each coin they collect!