From Test-Scratch-Wiki
当绿旗被点击 | |
当 @greenFlag 被点击 | |
类别 | 事件类积木 |
形状 | 半圆形积木 |
加入于 | Scratch 1.0 |
The When Green Flag Clicked 积木是一个事件类积木和一个半圆形积木。所有有这个积木的程式会在绿旗被点击时开始——而这些程式可以开启其他程式,从此而开始整个专案。
Without this block, the only way a project could run would be that it would sense the pressing of a key or clicking a 角色; the project would only last until all scripts depending on the starting scripts have ended. This block is also the most used block in all of Scratch.[1]
While it is fully possible to script projects without this block, it is not recommended; the 旗子 represents the start of a project, so this block will sense when the project has been started.
用法示例
When a 专案 is started, numerous things happen — these happenings occur at this time because they are hatted with this block. Some common uses:
当 @greenflag 被点击 删除第 (全部 v) 项 \( [list v] \)
- Resetting 变量
当 @greenflag 被点击 将 [variable v] 设定为 (0)
当 @greenflag 被点击 隐藏 造型换成 [造型1 v]
- Clearing all pen
当 @greenflag 被点击 笔迹清除
- Playing (a) song(s)
当 @greenflag 被点击 重复执行 播放声音 [歌 v] 直到播放完毕 end
- Creating 分身
当 @greenflag 被点击 分身 [自己 v] 建立
After these occurrences, the script can continue normally. For example, (with several of those occurrences):
當 @greenflag 被點擊 效果清除 定位到 x: (0) y: (0) 廣播訊息 [Start! v] 重複無限次 如果 <(meter) < (1)> 那麼 廣播訊息 [You Lose v] end end
Errors
Using more than one of these blocks in a single project can create errors that are difficult to diagnose, often only appearing with every other start of the project and can cause the timing to go off. A simple solution is to use only one of these blocks followed by a 广播 to all other scripts that need to run when the project begins. Another solution is to click the flag multiple times or use 加速模式.
當 @greenflag 被點擊 廣播訊息 [initialize v]
应用
- Main article: List of Block Workarounds
當 [計時器 v] > (-1) 說出 [Hello world!]
This is the only current workaround for the When Green Flag Clicked block. Scratch suppresses all 半圆形积木 when the project first opens, and when the green flag is clicked, the timer is set to 0, and the hat block is triggered. The only problem with this method is that it automatically triggers if a user goes into a project.
當收到訊息 [Scratch-StartClicked v] 說出 [Hello, world]
Before Scratch 2.0, this workaround could be used because clicking the green flag broadcasted "scratch-startclicked".[2] However, it no longer 广播 the message, so this workaround will only work in Scratch 1.4 and below, as well as the Java 播放器.
When Stop Clicked
"When Stop Clicked" is an 事件类积木 requested by many Scratchers, that triggers when the stop sign is pressed. These requests are usually turned down, however, because the stop sign was made to stop the project, not to trigger other scripts.
A workaround for the "When Stop Clicked" block uses the following code:
定義 check 重複無限次 計時器重置 // resets the timer every tenth of a second to keep it below 0.2 等待 (0.1) 秒 end 當 @greenflag 被點擊 check :: custom 當 [計時器 v] > (0.2) // when stop is clicked, the timer will not reset, allowing its value to rise above 0.2 . . . // what is done when stop is clicked check :: custom