Breakable loop in Scratch?
Asked Answered
D

6

8

How do you make a breakable loop in Scratch? I'm using Scratch 2.0 and can't find any good way to make a loop breakable, from inside of the loop itself.

Duthie answered 6/6, 2015 at 11:16 Comment(0)
D
13

Disclaimer:

There is no perfect way to do it. If you can possibly stand this true fact then feel free to continue.


There are a few different ways you could do it.

With repeat until

The first and most simple one follows this:

scratchblocks

But this isn't technically part of the script - it's just repeating until some value returns true.

With a custom block (stop this script)

In order to do it inside of the script, you'll need to use a sneaky little trick with custom blocks.

Create a custom block called whatever you want - but probably along the lines of "breakable loop". Inside of it, create this script:

scratchblocks

By using stop script we are breaking out of the script that is currently running - which, according to Scratch, is the custom block.

See the result! (as scratchblocks)

With broadcast and wait

You could also use a broadcast-and-wait method, very similar to above:

scratchblocks

Though I highly suggest you don't use this method, as if any other sprites have breakable loops you'll need to rename each one, which can be tedious after using a lot of loops in a lot of sprites!


(Note this bug has been fixed in version 442 of the editor and such the following no longer applies.)

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.

Duthie answered 6/6, 2015 at 11:16 Comment(0)
H
4

You can make a variable inside or outside of the repeat and make your script like this:

repeat until [[my variable] = [e.g: 1]]
your code
your code
your code
your code
end of repeat until
Hadst answered 10/12, 2015 at 18:36 Comment(1)
Oh yes, now I feel silly for not thinking of this. I do like this solution. :)Duthie
A
1

For a "repeat until" block the simplest way would be to "or" your normal until condition with the break condition in the until.

By adding an incremeting loop counter variable in the loop you can use a "repeat until" to replicate the function of a "repeat n times" block

By using a "repeat until" block with only your break condition you get the equivalent of a "forever" block

If you need another script/ sprite to trigger the break then a public variable will let you break the loop from anywhere and let a single condition break loops for different sprites.

I'd post an image of the blocks but this is my first reply and the site won't let me!

good luck

Absurd answered 10/6, 2015 at 15:18 Comment(2)
You could use Scratchblocks to make a picture. ;) The problem with this is we aren't technically breaking out of the loop from inside the script; we're just looping until some conditional returns true.Duthie
If it matters to break out mid loop (an odd thing to need in Scratch), and the last iteration of the loop is not undoable in the post loop blocks, then the if <condition> stop script route may be only way to go. Broadcasts aren't pretty but they'd be my fallback over custom blocks which I have found to be less easy to explain to some kids.Absurd
R
1

You can use these few ways to do it...

  • conditional loop

  • stop this script

  • if then else, in the else section, put nothing

I would prefer to use the first method, as it requires less blocks and for the first method, you can still add in code that will be executed after the loop has stopped executing.

Rhombus answered 4/10, 2019 at 13:4 Comment(2)
Do you think you could share some examples? The "if then else" one is particularly interesting since I don't think I've heard of it before, but your answer would be a lot more useful to readers with examples all around :)Duthie
Please add an example for each of the methodsInversion
H
0

You can make it repeat x times or make it have a certain point where it stops, such as another variable changing.

Otherwise, I don't think there is a wat to do that.

Hedonic answered 9/5, 2017 at 20:11 Comment(0)
S
0

Use the repeat until block. Then put in an equals block or whatever into the boolean part. Then inside that repeat until block, put a stop this script block.

Hope this helps :D

Snorter answered 30/7, 2019 at 1:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.