Scratch 中的可破坏循环?

Breakable loop in Scratch?

如何在 Scratch 中制作可破坏的循环?我正在使用 Scratch 2.0,但无法从循环内部找到任何使循环可破坏的好方法。

免责声明:

没有完美的方法。如果你能忍受这个事实,那就继续吧。


有几种不同的方法可以做到这一点。

repeat until

第一个也是最简单的一个如下:

但这在技术上不是脚本的一部分 - 它只是重复直到某个值 returns 为真。

使用自定义块(stop this script

为了在脚本中执行此操作,您需要对自定义块使用一些偷偷摸摸的小技巧。

创建一个自定义块,随便起什么名字——但可能与“可破坏的循环”类似。在其中,创建此脚本:

通过使用 stop script,我们打破了当前 运行 的脚本 - 根据 Scratch,这是自定义块。

See the result! (as scratchblocks)

broadcast and wait

您也可以使用广播等待方法,与上面非常相似:

虽然我强烈建议您不要使用此方法,因为如果任何其他 sprite 都有可破坏的循环,您将需要重命名每个 sprite,在大量 sprite 中使用大量循环后,这可能会很乏味!


(注意此错误已在 442 版编辑器中修复,因此以下内容不再适用。)

Help! My project is lagging a bunch now!

As @foi has noticed, if your code must be run inside of a frame you probably checked run without screen refresh. Unfortunately, due to a bug in the Scratch player, this causes the program to essentially break after the stop this script block has been activated. How can you handle this?

It follows the same principle you use when you use a run without screen refresh custom block inside of a forever loop - the loop doesn't use screen refresh while the inside does, allowing for instant animations whether or not one is using turbo mode.

Here's an example - the image is really too long to be embedded, so see it here instead.

对于 "repeat until" 块,最简单的方法是 "or" 您的正常 until 条件和 until 中的中断条件。

通过在循环中添加递增循环计数器变量,您可以使用 "repeat until" 复制 "repeat n times" 块的功能

通过使用只有你的中断条件的 "repeat until" 块,你得到相当于 "forever" 块

如果您需要另一个脚本/精灵来触发中断,那么 public 变量可以让您从任何地方中断循环,并让单个条件中断循环用于不同的精灵。

我想要 post 积木的图片,但这是我的第一个回复,网站不允许我!

祝你好运

您可以在 repeat 内部或外部创建一个变量,并使您的脚本像这样:

repeat until [[my variable] = [e.g: 1]]
your code
your code
your code
your code
end of repeat until

你可以让它重复x次或者让它在某个点停止,比如另一个变量改变。

否则,我认为没有办法做到这一点。

使用重复直到块。然后将 equals 块或其他任何内容放入布尔部分。然后在 repeat until 块中,放置一个 stop this script 块。

希望对您有所帮助 :D

你可以用这几种方法来做...

  • 条件循环

  • 停止此脚本

  • if then else,在else部分什么都不放

我更愿意使用第一种方法,因为它需要更少的块,而且对于第一种方法,您仍然可以添加将在循环停止执行后执行的代码。