Skip to main content

Messages

We can communicate two programs either within the same sprite or between different sprites or with the stage by sending messages. A message can be sent from a program from one sprite to another, to perform some action on it. These blocks are located in "Events".

imagen.png

Practical case:

Open a new file and include two sprites: one cat and one dog.

Add programs to the cat so that you can move it with the keyboard. (see motion section)

We are going to make it so that when the cat touches the dog, the cat program broadcasts a message to the dog program. When the dog receives the message, it will change its position, to a random position.

Solution


When Flag clicked, if the Cat  touches the dog (Dog1), it sends a message.


Program to include in Cat code:

imagen.png

That message is received by the Dog1 sprite, and  makes the dog move to a random position on the screen.

Program to include in Dog1 code:

imagen.png

Save it with the name messages.sb3