From Test-Scratch-Wiki
- For the block named "Broadcast", see Broadcast () (block).
A broadcast is a message that is sent through the Scratch program, activating receiving scripts. Broadcasts are sent with the blocks Broadcast () and Broadcast () And Wait, and are received by the hat block When I Receive ().
Broadcasts are useful in games and animations, as they trigger specific scripts. They are similar to events, which are scripts triggered when certain actions, like mouse moves or key presses, are performed.
广播代码块
所有广播块都可以在Events找到。在Scratch 1.4和更早, 它们在Control板块。
广播()
- Main article: Broadcast () (block)
广播 [message1 v]
这个block 进行指定的广播,并等待。
广播()并等待
- Main article: Broadcast () and Wait (block)
广播[message1 v]并等待
该块进行指定的广播并等待script直到所有接收脚本都完成为止。
当接收到()
- Main article: When I Receive () (block)
当接收到[message1 v]
该代码块将保持不活动状态,直到收到指定的广播为止。 收到后,script开始执行并在完成后结束,但是可以多次启动。
() Received?
<[something v] received?::sensing>
<[something v] received?::control>
<[something v received?::events>
The () Received? (or I Receive ()) block is a non-existent Boolean block that many Scratchers have requested. This block would most likely belong in the Sensing category, or possibly the Control category. It was rejected by the Scratch Team for being too ambiguous.[1]
当接收到 [broadcast v] 将 [broadcast_received v] 设为 [true] 等待 (0.1) 秒 将 [broadcast_received v] 设为 [false] 当绿旗被点击 重复执行 如果 <(broadcast_received) = [true]> 那么 ... end
The code works by using the variable broadcast_received to keep track of when a broadcast is received. When a broadcast is received, broadcast_received is set to "true" for long enough that all scripts will run at least once. Hence, all scripts can check broadcast_received and see that a broadcast was received.
Broadcasting to Specific Sprites
Scratch cannot limit where a broadcast can go; however, it is fairly simple to work around this with a global variable containing who is to receive the broadcast, and private variable for each sprite containing that sprite's ID. If the ID and receiver match for a specific sprite, the broadcast is carried out, otherwise the broadcast is ignored by the sprite.
点击绿旗时 // On the Stage 广播 [init v] 当接收到 [init v]// On sprite 1 set [MyID v] to [s1] 当接收到 [init v]// On sprite 2 set [MyID v] to [s2] 当接收到 [init v]// On sprite 3 set [MyID v] to [s3] 当接收到 [broadcast v]// On each sprite if <(_receiver) = (MyID)> then say [Me, right?] for (0.5) secs end when gf clicked// to send a broadcast forever set [_receiver v] to (join [s] (pick random (1) to (3)))// set who receives broadcast broadcast [broadcast v] and wait// broadcast
It is possible to automatically set the MyID variables rather than change the value for each sprite. This is useful when there are many identical sprites, for example, if there are many bubbles floating on the screen. To do this, change the "init" broadcast handler to the following (create the global variable "IDconstr"):
when I receive [init v] set [MyID v] to (IDconstr) change [IDconstr v] by (1)
This takes advantage of the handler being Single Frame, so Scratch threads the scripts atomically in a pseudo-random order (see Execution Order). Thus, each sprite receives a unique random value of MyID.
Execution Order (1.4)
In Scratch 1.4, using the 'broadcast' block, the execution order of broadcasts is determined by the last dragged sprite, on all platforms, as can be viewed here.
If a sprite has two scripts with the same broadcast trigger, the last dragged script will be executed first on Scratch 1.4, but last online.
Caution: | This info is implementation dependent, and could be changed in further versions. As this is not an official Scratch feature, this should not be depended on in projects. |
Scratch Extension Features
Some Scratch Extensions provide special usages of broadcasts.
Mesh
- Main article: Mesh
When Scratch projects are in a Mesh session, broadcasts can be sent on one project and received on the other. This allows for great communication between projects.
Clutter
- Main article: Clutter
In a story clutter, broadcasting ->
or <-
goes to the next or previous project, respectively. In a link clutter, broadcasting ->project name
goes to the specified project. In a secret word clutter, broadcasting ->
goes to the next project.
捷径
This article or section documents a feature not included in the current version of Scratch (3.0). It is only useful from a historical perspective. |
- Broadcasting "scratch-startclicked" will simulate clicking of the Green Flag. It is commonly used in replay buttons in games and animations, where clicking a sprite broadcasts this message to replay the game or movie.
- Broadcasting "scratch-mouseclickevent" clicks all sprites
- Broadcasting "scratch-keypressedevent" check whether any hat blocks to do with key pressing (i.e. When () Key Pressed) are being used, then produces a script error
References
- ↑ jvvg. 5/10/13. "There is way too much ambiguity to how this would work." http://scratch.mit.edu/discuss/topic/4789/