Table of Contents

Enable a Device to Interact with a Sprite

Enable a Device to Interact with a Sprite

In addition to separate device and stage programming, mBlock 5 also supports the interaction between devices and sprites. By using the following broadcast message blocks, you can enable a device to interact with a sprite, which allows you to have more fun with programming.

image

In the programs described in previous sections, we have created a bee and added a sound and costume for it, and programmed Halocode.

Now, let’s try to enable Halocode to interact with the bee: when you press the blue button on Halocode, the bee on the stage moves.

Program the device

(1) Drag the Events block when button is pressed to the Scripts area.

(2) Drag the Events block broadcast ( ) to the Scripts area.

image

(3) Create a new message. You can set a name for the new message to help identify it. In this example, let’s name it move.

Program the sprite

(1) Continue to compile the program described in “Add Sounds and Costumes“. Drag when green flag clicked back to the Blocks area to delete it.

image

(2) Drag the Events block when I receive ( ) to the Scripts area and select the message move.

image

(3) Press the blue button on Halocode and see what happens.

Note: For more details about how to enable devices to interact with sprites, see Interact with Sprites .