Learning MIT Scratch : Stage Variables

Satish Goda
2 min readJun 26, 2019

--

About

In MIT Scratch Programming language, variables created on a Stage are available to all the sprites.

To create a variable on a Stage

  1. Select the Stage
  2. Navigate to the “Variables” blocks.
  3. Click on “Make a Variable” button
  4. Give a name to your variable.

Using one Stage variable, the following interactive app was created using MIT Scratch.

Stage

Variables

A variable called “drawing_in_progress” is created with a value of 0. This variable is set to 1 by the sprite that is being drawn.

Blocks

  1. When the flag is clicked we are going to erase all the Pen drawings
  2. When the Stage is clicked and if no drawing is currently in progress (checked using the Stage variable “drawing_in_progress”), we broadcast a message called “new position”. The sprite is going to receive this and act accordingly: Move to the new position, set the Stage variable “drawing_in_progress” to 1 and draw the shape with its pen.

Sprite

Blocks

  1. Select the ‘Cat’ Sprite
  2. Enable the stage global variable (“drawing_in_progress”) so that we can see its value on the canvas.
  3. Setup the initialization blocks for the sprite (Initialization happens when the Flag is clicked)
  4. When the sprite receives a “new position” message, it will move to the location of the mouse pointer, stop speaking and then draw a square shape with its pen (by sending itself a message “draw square”)
  5. Blocks for drawing the square shape.

Demo

Check the following video see the application in action.

--

--

Satish Goda

...imagine...create...express...share...enjoy... (Educator / Writer/ Engineer / Artist)