Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

Find out more

Interaction between sprite code

Scratch includes Broadcast blocks which you can use to coordinate the actions of multiple sprites. Read how to use them here.

As you develop your projects in Scratch, you may decide to use several sprites. To allow you to make the code on your different sprites interact, Scratch includes three blocks designed to let you send messages between them. By using these blocks you can coordinate the actions of multiple sprites, for example in a group dance on the Stage.

Broadcast blocks

The broadcast blocks allow you to program a sprite so that it sends out a specific message that all other sprites can receive. The when I receive block allows you to program a sprite to listen for a specific broadcast message. You can program multiple sprites to listen for the same broadcast message allowing them to all respond at the same time.

To set the message to be broadcast, you will need to type it in using the drop down option on the broadcast blocks. Once you’ve entered a broadcast message, it will appear in a list as a selectable option in the drop-down of the when I receive block.

broadcast (my message v) and when I receive (my message v) blocks

The broadcast and wait block works in a similar manner to the broadcast block, but it also waits until all code activated by the broadcast ends and stops running. After any activated code has stopped, the code you’ve attached after the broadcast and wait block will run.

broadcast (my message v) and wait block

Using Broadcast blocks

To demonstrate how the broadcast process works, I’ve created a Scratch project in which I’ve added a sprite to the Stage which acts as a button. When you click on the Button sprite, the Pencil sprite draws a square on the Stage.

First, I added the Scratch Button3 sprite to my project. I then used the Paint editor to add the word “Square” to the sprite’s costume. Next, I added the blocks when this sprite clicked and broadcast to it. I changed the broadcast message to “Draw a square”.

Scratch blocks - When this sprite clicked broadcast (Draw a square v)

To the Pencil sprite, I’ve added a when I receive block and set the message “Draw a square” from the drop-down options on the block. I then added a square block to it (I defined the square block in the previous step).

Scratch blocks - When I receive "Draw a square" with square block attached.

In my project, when you click on the Square button, a square is drawn on the Stage.

Screen recording of the Scratch Stage. The mouse moves to a button labelled square and clicks it. The Pencil sprite then draws a square on the Stage.

Task

Create your own drawing program. You should try and include ways that the program user can:

  • Move the pencil around the Stage
  • Automatically draw a shape from the location of the pencil
  • Clear the screen and start again

In your project you should try and use the blocks which I’ve introduced this week, which are:

  • Repeat ( ) times
  • My Blocks
  • Broadcast and when I receive

You can also use anything you learnt in the first week of the course.

Finally I’d like to see how you extend your program by bringing in other features such as pen colour or thickness.

If you would like some help to get you started, you can use this starter project.

Share your new program:

This article is from the free online

Introduction to Programming with Scratch

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now