in this lecture, we are going to create the coin 2 2
let's create a scene for the coin 3 3
hit the plus icon at the top, then hit 2D scene to the right to create a Node2D root node 4 4
Let's rename the root node to Coin 5 5
then let's change its type to Area2D 6 6
let's press CTRL + S to save the scene into the Game / Scene folder 7 7
Godot names the scene coin.tscn, and let's just leave it as it is 8 8
next, let's add a CollisionShape2D node to the root node 9 9
then let's create a CircleShape2D for the shape 10 10
the default Radius for the circle should be 10, let's leave it be 11 11
next, let's add an AnimatedSprite2D node to the root node 12 12
create the SpriteFrames and rename the default animation to Idle 13 13
next, let's add the frames, go to the Asset / Animation / Other folder and select the Coin.png file 14 14
make sure the grid is 6 by 4, then select the first two rows from left to right, top to bottom 15 15
in total we have selected 12 frames, then let's hit Add 12 frames 16 16
let's change the frame rate to 10, make sure the animation looping option is enabled 17 17
let's also enable Autoplay on Load, so this animation will start playing when the game starts 18 18
the blue circle collision shape sits right at the origin of the root node, but the coin is slightly offset downward 19 19
so let's bring the coin up by setting the y-position of the AnimatedSprite2D node to -5 20 20
next, let's add an animation and call it Collected 21 21
let's add the frames, select the same Coin.png file, 22 22
make sure the grid is 6 by 4 and select the last 10 frames, starting from the leftmost one in the thrid row 23 23
let's change the frame rate to 15, and this time we are going to disable the animation looping option 24 24
now we have all the nodes we need, let's add a new script to the root node 25 25
let's change the name to Coin.gd, starting with a capital C 26 26
then let's keep the script file in the Game / Script folder 27 27
before we start writing the script, let's select the root node(the Coin node), set the Collision Layer to nothing, and the Collision Mask to 2 28 28
so this Coin can only be picked up by the player 29 29
then let's switch to the Node panel, and connect the body_entered() signal to the Coin script 30 30
let's select the Coin node and hit Connect 31 31
great, now we have the signal receiver function in the Coin script 32 32
we also need to bring in the AnimatedSprite2D as a variable, so let's drag and drop it into the script while holding the CTRL key 33 33
next, let's delete the pass line in the receiver function 34 34
then we want to play the Collected animation once the player touches this coin 35 35
next, we need to call a function on the Player script, so let's try to cast the body parameter as the PlayerController class 36 36
then let's check if the casting was successful by checking if the player variable exsits 37 37
then we can call the function on the player script, but wait a minute, we don't have a function to call, right? 38 38
all right, let's go to the Player script to create a function first 39 39
at the bottom, let's create a function called CollectedCoin() 40 40
and let's declare an int parameter called value in the parenthesis 41 41
in the function, let's simply print out the value parameter to see if the funciton works 42 42
now save the Player script and to back to the Coin script 43 43
let's declare a variable to define how much value the player can get from collecting this coin, let's call the variable value and set its default value to 1 44 44
then we can call the CollectedCoin() function on the player variable and pass in value as the parameter 45 45
one more thing, we want this coin to destory itself when the Collected animation is finished playing, let's do it in the _process() function 46 46
let's check if the AnimatedSprite2D node is playing an animation 47 47
it should be true when the Idle animaiton is playing since the Idle animation is looped 48 48
but when the Collected animation is playing, it will stop in the end and we know it's time to remove this coin from the game 49 49
in the if block, let's use the queue_free() function to remove the current coin from the game 50 50
we are almost done here, let's me just show you one more thing 51 51
you can add the @export annotation at the front of a variable, expose the variable in the inspector 52 52
we need to save the script for the annotation to take effect, then let's select the root node 53 53
go to the inspector, you'll see a property called Value appears, then you can change its value to test the game without editing the script 54 54
this trick is not necessary here, but I just want to show you an easy way to change the variables in the editor 55 55
all right, we are all done here, let's close the Coin scene and go back to the Main scene 56 56
let's instantiate a few coins in the scene, this time let's just find the coin.tscn file in the Game / Scene folder, and drag it into the scene 57 57
then let's select the Coin node and press CTRL+D to duplicate two more coins, and place the new ones to the right 58 58
all right, let's test our game 59 59
seems like the script is working, and we can see the messages in the console window after we stop the game