Six Scratch troubleshooting tips

Using the Say() Block as a Check

Sometimes you will want to check the value of a calculation, a boolean, or the value in a reporter block as the project runs. A very easy way to do this is with a say () block. Figure 3 shows the correct version of the script in Figure 2 with two say () for () secs blocks added.

Figure 3: An example using the say blocks to check values as the project runs.

Normally, you wouldn't need both of the say blocks that I used in my example, but I wanted to show that you can add text comments into the say block or use any reporter block as the value. The first example I used in Figure 3 simply gives a visual message of "smaller" each time the repeat loop runs. The second example displays the size of the sprite. Which option you use depends on how specific you need to be with your troubleshooting. You can also add a boolean block as the value, and the say block will evaluate the boolean and show whether it's true of false.

Resetting Starting Values

Take a look at the script in Figure 4. This script will end the game when the sprite's health is equal to 5. However, if you play another game based on this script, you will notice that the game ends without the chance to play.

Figure 4: Ending the game.

In this example, I'm using a variable named health, and in Scratch, all variables have a reporter block. You could click on the health block in the Variables palette to see the stored value, or you can check the box and display the monitor on the stage. Either way, you'd notice that the game is starting with a health of 5 – and there is the problem.

To fix it, you need to initialize the starting values of the game, which in this case would be a set (health) to (0) block.

This is a common scenario any time you create a project that keeps score, but it's also common when you need a sprite to start in a specific location, look a certain way, be a specific size or color, or hide each time the project starts. You get the idea.

You can apply this idea to testing levels in a project. For example, if you have a game with five levels that change based on score, then you'll want to test each level of the game as you develop it. However, it's unlikely that you'll want play through the game each time just to test your level. So, you can initialize a starting score that will take you to the correct level. Don't forget to take out this testing code when you finish your project.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content