1 00:00:00,000 --> 00:00:13,009 Have you seen the My Blocks category in the Scratch Blocks palette? 2 00:00:13,009 --> 00:00:17,010 When you click on it, no blocks are present until you create one. 3 00:00:17,010 --> 00:00:22,010 Let's explore how to create a customized block, and when and why you might want to use them. 4 00:00:22,010 --> 00:00:28,010 Let's say you are creating a Scratch program, and you've written a sequence of steps that performs a particular action. 5 00:00:28,010 --> 00:00:33,009 That action could be performing a complicated spin move, playing a set of musical notes, 6 00:00:33,009 --> 00:00:38,109 drawing a shape with the pen tool, performing some complex math, or anything 7 00:00:38,109 --> 00:00:44,009 else. That stack of blocks is known as a procedure or a routine. Most computer 8 00:00:44,009 --> 00:00:47,710 programming languages allow you to create a special name for a group of 9 00:00:47,710 --> 00:00:52,270 commands or lines of code and then call for that sequence to run at any point in 10 00:00:52,270 --> 00:00:58,289 the main program. In Scratch, we use my blocks. Click on my blocks in the blocks 11 00:00:58,289 --> 00:01:02,250 palette and then click on make a block. You can give your block any name you 12 00:01:02,250 --> 00:01:06,909 want, but I'd recommend something descriptive, so you can recall later what this stack of 13 00:01:06,909 --> 00:01:07,909 blocks does. 14 00:01:07,909 --> 00:01:12,510 Let's say I want to create a series of blocks that makes my cat sprite spin forward and 15 00:01:12,510 --> 00:01:13,510 backward. 16 00:01:13,510 --> 00:01:15,969 I'll call my block Cat Spin. 17 00:01:15,969 --> 00:01:20,670 Once I click OK, you'll see a new Event Handler block appears on the Scripts area 18 00:01:20,670 --> 00:01:22,689 that says Define. 19 00:01:22,689 --> 00:01:27,129 Under this block, I want to add all the code blocks in my procedure. 20 00:01:27,129 --> 00:01:32,189 For this example, I've added some repeating code that makes the cat whirl and turn forward 21 00:01:32,189 --> 00:01:36,909 in two complete circles, and then reverse the world, turning in the opposite direction. 22 00:01:37,709 --> 00:01:39,790 Let's click on this stack and see how it looks. 23 00:01:41,900 --> 00:01:45,980 Now that I have defined what my custom block does, let's use it in my main program. 24 00:01:46,540 --> 00:01:50,459 When I want the spin to occur, I'll grab my custom cat spin block, 25 00:01:50,459 --> 00:01:54,060 now available in the Blocks palette, and place it in my main program. 26 00:01:54,620 --> 00:01:58,700 In this program that I'm writing, the cat will spin after each background change, 27 00:01:58,700 --> 00:02:00,219 and say something after the spin. 28 00:02:00,219 --> 00:02:09,759 Let's see how this looks by clicking the green flag. 29 00:02:09,759 --> 00:02:14,340 The advantage of writing this procedure separately is that I can use one block to call for a 30 00:02:14,340 --> 00:02:18,400 whole set of steps to be run each time I need it. 31 00:02:18,400 --> 00:02:22,620 Creating separate procedures using custom blocks makes the code faster to write and 32 00:02:22,620 --> 00:02:25,719 read, and easier or quicker to edit. 33 00:02:25,719 --> 00:02:29,979 If I wasn't using a custom block, this is how the code would look, looking at them side 34 00:02:29,979 --> 00:02:36,439 by side, which is easier to read. 35 00:02:36,439 --> 00:02:41,939 And look, if I wanted to change the spin, I only have to edit it in one place, versus 36 00:02:41,939 --> 00:02:46,639 editing it in multiple places in my long, single code sequence. 37 00:02:46,639 --> 00:02:49,979 You don't have to use a custom block just for code that will repeat.