Messages

Sprites react to key presses or to touching other sprites or colours. A forever loop can also contrain logic to test for certain positions on the stage and so forth. A practical means of communicating between all the object in the applicatiuon (including the stage) is for a sprite to broadcast a message.

One technique is to broadcast a message when a sprite reaches the edge of the stage to cause the background picture to change to the next room. This can be achieved by placing a single pixel of a unique colour onto the background at the point where the entrance to the adjoining room occurs. A forever loop allows a test for the sprite touching this unique colour which then broadcasts a message received by an event conrtol on the background which changes the background.

The sprite logic is as follows and presented in the messages application:

Clicking on the stage icon in the design environment allows program blocks to bne defined for the background. The broadcast messages can be used to move from room to room in the Scratch application:

Broadcasting can be used to communicate between sprites and allow sprites to be used as buttons for example by broadcasting a message to all sprites using the sprite clicked control block for the button sprite.

Another technique allows the backgound application to monitor a variable against a second variable contining the previous value of the variable to broadcast a message when the slider is used to change the value. The following code is added to the forever loop of the stage:

Any sprite that needs to respond to a change in the value of the slider can wait for the relevant message to be broadcast:

19/11/2017 12:04:38