 I'm sensing some curiosity around sensing blocks in Scratch. We'll stick around because in this Scratch Sensing Tutorial we'll explore each block through a worked example. That includes color sensing, basic collision detection, and handling user input through Scratch Ask and Answer. All this, come in your way. Hello world, Surfing Scratcher here, a teacher-surfer programmer bringing you the goodness of learning to code through video tutorials. If it sounds like something that you're into, then consider hitting that subscribe button. Check out the show more button down below for links that relate to this video. I've also got a link to my funky tea here. This is Cinnacea that you can find on Redbubble. If you sus out the link in the description below and purchase it, you'll be supporting this channel. But hey, I'm sensing it's time to get into it. All right, here we are in the Scratch Project Editor. This project is our Scratch Sensing Blocks. And I just want to give you a quick orientation. We've got two sprites here. We've got a target sprite and we've also got an arrow sprite, which you can see located down here. The idea is we're going to get this arrow to hit the target on a click. So we're just in the arrows sprite here. And I've just set up some code to begin with. So we've got some motion blocks and some books blocks here, which is always going to send the arrow to the front. And we're going to give it a starting position when the green flag is clicked. We've got a forever loop and we've also got an if block in here. So you can go check out the control box tutorial if you're a bit fuzzy on those. The first sensing block that we're going to check out is this mouse down block here. What that does is it detects whether the user's primary mouse button is held down. So it's not a click, it's held down. So you'll see if I click it here, it will say false. So we're just going to put this Boolean block inside our if statements. So we want to check if the mouse is down on the stage. Now I'm clicking the mouse button down now and nothing's happening because we're not doing anything. So what we need to do is send the arrow to the current mouse X and the current mouse Y. We've got two reporter blocks to help us out with that. We've got the mouse X and the mouse Y. Let's go into our motion blocks and grab a go to X and Y position. Let's now put in the mouse X and the mouse Y. Watch what happens when I click on the stage now. So I'm holding the button down and our arrow is going directly to where our mouse positions are, the X and the Y. But let go of the mouse. The arrow is no longer pinned to the mouse X and Y. It's no good just clicking on the stage and having our arrow go directly to where our mouse is. Go on ahead and create a little animation and put it in a custom block called shoot arrow. Okay, so here is shoot arrow and you can just have a little look down this list here. All I'm doing is basically gliding to the mouse X and the mouse Y position when we click down on the stage. If we do that now, you can see our arrow shoots into position. We're going to check out these top three boolean blocks now. And I've just jumped over into the target sprite and I've got this code, the Scallowing code here. So these three blocks are great to detect, both user input and collision detection. I've just gone ahead to grab a stack blocks just to show us what is happening with all these boolean blocks. So we drag out this touching boolean block here and we put it inside the statement. So see if we're touching the mouse pointer, so if I press the green flag and I drag my mouse over the target, you'll see here that it is hitting. We've also got a drop down menu here and we can sense if our sprite is touching the edge of the screen. So if I go ahead and drag my sprite to the edge of the screen and I drop it, now it is sensing that it is touching the edge of the screen. That works for everywhere on the four edges here. The other one is if we get our arrow and it is colliding with the target, then we can detect a collision between the two. Just gone ahead and refactored some of this code. I've got rid of the forever loop and we've got an event that we're firing once the arrow has finished animating. It is check target. So if I press the arrow here and you'll see that for one second it says that we are touching the arrow. If we look down here at the arrow, it might be a bit hard to see. I'm going to jump into the costume and you'll see at the arrow tip we have got this little white arrow tip that we're going to be using to detect a collision between colors. So let's jump back into the code. So let's grab this touching color boolean block and what this does is it sends if our target is touching a color on the stage. So if we drag that in and we modify the color to that white of the arrow tip and then we fire the arrow, you'll see that the board has now been hit. Our target has different colored zones here and we can use the next boolean block to detect if our white arrow tip hits one of these target zones. Let's first detect if our arrow tip hits this gray area. So I'm going to drag in this boolean block. The color that we want to detect is the color of the gray area and we can just use that color picker tool to select that color. And we want to detect if it's hitting the white arrow tip. So if I go ahead now and fire the arrow and it lands in the gray area, we get the feedback to say it hit the gray area. So I've just gone ahead and done the exact same thing for the other colors of the target. So I go and fire the arrow into that light gray area here. We get that feedback into the bluey area and also into the red area. So that's really neat how we can detect different collisions between different colors. Lastly, the order of the colors is important. So the colors on the left-hand side refer to the colors of the current sprite and the other color refers to another color on the stage like in a different sprite or on the backdrop. Okay, let's check out this distance to reporter block. And what this does is create a straight line between the sprite center point and whatever other objects center point. So for instance, this is going to measure the distance between the mouse's center point and the target center point which is these two here. So if we click it, it's not going to be very helpful because the mouse is all the way over here. So what we can do is get the dropdown menu and let's measure the distance between the center point of the target here and the tip of the arrow which is the center point of the arrow. And that is 259 pixels. We bring our arrow a little bit closer and we click it. It is now reduced to 106 pixels. Okay, let's check out the next two blocks. It's an ask and wait block and this answer block. I'm just going to click this flag so that you can see the answer up on the stage. That'll be a little bit clearer in a moment. So if we click this block, it prompts the user to enter in some text here. So we could type in our name and once we press the tick, our answer displays in a variable here, a reporter block that we can use. Now, if we click it again, the only way to get rid of this text box is to click the stop sign here but there is a little bit of a work around that we can use. I'm just going to show that now. Use the next Boolean block in the sensing category to check this out. So we've got this key press Boolean block and what this does is we can tell if a user is pressing down a key. So let's get the up arrow and slot that inside of our if statement. I've gone ahead and created an event and this event is called ask and what that will do is it will fire a message to other sprites who are listening, this ask event. If you get fuzzy on events, go check out the card in the top corner now. Going to jump across into this cancel sprite that I've done. So this cancel sprite here is that ask event and the first thing it does is it shows itself and then it asks what is your name and it waits. So if I press the up arrow key now, you'll see that it is now asking what is your name. If you want this text inside this window here, make sure that you ask your stage to ask and wait for this particular text here. So when the cancel button is now clicked, we will hide the cancel button and we'll stop other scripts in the sprite. So what that will do is it will stop these scripts. So not this one, but the other scripts that are in this sprite. So I'm going to click this cancel button and it goes away. Boom. Now sometimes when we fire our arrow, this target tends to move and we don't really want that to happen. So what we can go ahead and do is get this set drag mode block. And what this will do is it will set whether or not a sprite can be draggled. So here we are to draggle and to not draggle. So I've got that there, I've pressed it and I'm going to, oh no, look, the target still moves. Now what you need to be aware of is that this only works inside full screen mode. So if we go into full screen mode now, I cannot click and drag that target. Let's check out these reporter blocks. So if we click the ladders flag, we'll see that as I talk into the microphone, we're getting some numerical values. If I don't say anything, it goes back down to zero. So this is a value between zero and 100 with 100 being the loudest. We also have this timer value and what that does is it is the time from which you press the green flags. If I press the green flag here, you'll see that we have reset the timer. And a nice way to do that inside your project is to use this stack block where you can just click this stack block and we will reset the timer as you just saw then. This reporter block is very handy. It reports on the values or attributes of other sprites. So here we can check out the backdrop number of the stage, the name, the value, and also the values of some variables here. We can also change it to a particular sprite and we can check all the attributes of those sprites. So here is the exposition of the arrow. It is currently 200. If we go over to the arrow, you'll see that there it is there in the exposition. We can also detect the Y position. We can detect the direction, costume number, name, size and the volume. So this is a very handy reporter block. The next block's a reporter block and it is this current block. And what that does, it reports on the current time attributes of your local computer. So whatever time and date your local computer is set to. So here is the local hour of minus 24 hour time. So it's 10 in the morning where I am. It will also report on the current minute and all these other fun values as well, the current month, which is August. You can also use this next reporter block to check out the days since 2000. And you'll also get a fractional value of the current day. So if I keep pressing this, you'll see that the fraction value keeps updating. So this could be useful to add some features or release some features at a particular time or using it as a countdown clock. The last reporter block here reports on the username of the current user. So my username is surfing scratcher and that is what it prompts. If you have this flag used throughout your project, it will on your project page tell you that this project checks your username. Start for scratchy question. There was a moment where we went full screen and the arrow was landing behind the target. Can you suggest a way to fix the bug in the code? Drop your answer in the comment section below. Hey, thanks for checking out this scratch sensing tutorial. Like, subscribe, ring the bell if you're new around here. Have a scout of some other content on your screen right now. You can show your support for surfing scratcher by checking out some links in the description below. My Patreon page, RedbubbleTeas and even joining the mailing list. But until then, I'm off to go find a wave. I'll catch you in the next one.