Variables

Variables are used to store information for use in programs.

Scratch can store only numeric values in a variable which can be dropped in to any program block space with an oval shape. Make a variable in the variables page and set the tick box depending on whether you want to allow the user to view and change the value.

Many of the programming blocks have an oval slot where a number is entered to determine how the command will operate. Each of these slots can be replaced with a variable where appropriate. The example below (from project variables) uses the size variable to determine the size of a move command. The value of the variable is changed inside the loop so that the distance travelled increases each time the loop is executed. This allows the sprite to create a squiral shape.

Note that variables can be changed by the end user of the application if the checkbox remains checked in the variables page. You may position the variable on the stage and right click on it to set the range of permissable values which can be set by the user with the slider control.

Make sure the programmers can define and use variables within their sprite program. They should be able to create an applet that draws a shape using a size variable which can be changed by the user when running the program.

The speed of a sprite in continuous movement can be controlled by a variable defined solely for the sprite and not at the application level. The speed attribute of the main sprite in the variables application is adjusted with the up and down arrow keys or pressing space to stop the sprite. Pressing the left and right keys will alter the direction of the turtle and this type of movement is reminiscent of the computer game asteroids (also known as a dynaturtle in Logo).

19/03/2024 06:00:17