All that a Strawberry does is:
All that a Strawberry does is:
1.) Play its idle animation
1.) Play its idle animation
2.) Disappear when collected
2.) Disappear when collected
Add a script to the Strawberry node
Add a script to the Strawberry node
(1.) In the process function play the idle animation.
(1.) In the process function play the idle animation.
We will use Signals to detect collisions.
We will use Signals to detect collisions.
Select the Strawberry node
Select the Strawberry node
In the Inspector select the Node tab
In the Inspector select the Node tab
Then select the Signals tab
Then select the Signals tab
Connect the _on_body_entered() signal
Connect the _on_body_entered() signal
How do we know which body entered the strawberry?
How do we know which body entered the strawberry?
Return to the Player scene.
Return to the Player scene.
Select the Player node
Select the Player node
In the Inspector choose the Node tab
In the Inspector choose the Node tab
Then select the Groups tab
Then select the Groups tab
Click on the + sign to add a new group
Click on the + sign to add a new group
Name it Player and make it global
Name it Player and make it global
This is a way of tagging nodes so that we can identify them.
This is a way of tagging nodes so that we can identify them.
Add a function to increase the players score
Add a function to increase the players score
While we are here lets add a way to increase the number of strawberries collected by the player.
While we are here lets add a way to increase the number of strawberries collected by the player.
Add a new variable called strawberry_count
Add a new variable called strawberry_count
Now add a function called inc_strawberry()
Now add a function called inc_strawberry()
increase strawberry count by 1
increase strawberry count by 1
Return to the Strawberry scene.
Return to the Strawberry scene.
(2.) In the Strawberry script
(2.) In the Strawberry script
in the _on_body_entered() function
in the _on_body_entered() function
if the body is in the group called Player
if the body is in the group called Player
Call the players inc_strawberry function
Call the players inc_strawberry function
Destroy the strawberry with queue_free()
Destroy the strawberry with queue_free()
We can now collect strawberries and keep track of how many we have collected.
We can now collect strawberries and keep track of how many we have collected.
Scatter some strawberries throughout the level to collect.
Scatter some strawberries throughout the level to collect.