使用事件#
Events let separate stacks of blocks in a project communicate with each other, using blocks from the Logic - Events category. Blocks whose names begin with broadcast signal that an event has happened, and blocks whose names begin with when run their attached stack of blocks in response.
Broadcast an Event#
Add a Broadcast Block#
Add a broadcast block or a broadcast and wait block to your project.
Choose or Create the Event#
Select the dropdown on the broadcast block to choose an existing event, or to create, rename, or delete an event.
Receive the Event#
Add a when I receive Block#
Add a when I receive block to the workspace, and set its dropdown to match the event name used in your broadcast block. Then, attach blocks below it as those blocks will run whenever that event is broadcast.
Blocks starting with when can also be found with other device categories, running their attached blocks whenever that device’s condition is met. For example, the when Brain button and when Bumper blocks.
当Brain [◀ v] 按键 [按下 v]
当 [Bumper1 v] [按下 v]
Use Events in a Project#
Once a when I receive block matches the event name used by a broadcast block, running the project lets both stacks run at the same time. For example, this project turns until the Brain’s Check button is pressed, then broadcasts an event that stops the robot from driving.
当开始
[右 v] 转
等到 <Brain [确认 v] 键按下了?>
广播 [my_event v]
当我收到 [my_event v]
驱动停止