Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Stop an infinite loop!
- gwu777
-
New Scratcher
4 posts
Stop an infinite loop!
Hi there,
Sorry I know the title sound stupid, but I am trying to do a silly little game to learn scratch and I am getting frustrated with this one…
I have an elephant moving randomly accross the screen. I would like the elephant to stop when it touches the cat I am moving around. Been trying many things and the elephant just keep moving.
Any ideas?
Sorry I know the title sound stupid, but I am trying to do a silly little game to learn scratch and I am getting frustrated with this one…
I have an elephant moving randomly accross the screen. I would like the elephant to stop when it touches the cat I am moving around. Been trying many things and the elephant just keep moving.
Any ideas?
forever
glide () secs to x: (pick random (0) to (10)) y: (pick random (0) to (10))
end
- speckosaurus
-
Scratcher
100+ posts
Stop an infinite loop!
“Repeat until” works like a forever loop except it will stop when you meet the condition. For example:
The code that you want to execute after touching the cat will go outside your “repeat until” loop
repeat until <touching [cat v] ?>
end
The code that you want to execute after touching the cat will go outside your “repeat until” loop
Last edited by speckosaurus (Aug. 19, 2015 14:29:52)
- Zekrom01
-
Scratcher
1000+ posts
Stop an infinite loop!
Don't make it do an infinite, I think you should try to do this
(Cat is the sprite name.)
forever
repeat until <touching [cat v] ?>
glide () secs to x: (pick random (0) to (10)) y: (pick random (0) to (10))
end
end
(Cat is the sprite name.)
- Crimson19
-
Scratcher
1000+ posts
Stop an infinite loop!
forever
glide (1) secs to x: (...) y: (...)
if <touching [cat v]>
stop [this script v]
end
end
Last edited by Crimson19 (Aug. 19, 2015 14:28:25)
- gwu777
-
New Scratcher
4 posts
Stop an infinite loop!
forever
glide (1) secs to x: (...) y: (...)
if <touching [cat v]>
stop [this script v]
end
end
Thank you for all the answers, so here is the problem with all the above. I move the cat around with the arrows fine. When I use the above on the cat, as soon as the cat sprite touches the elephant, then the cat say “You lost” and I made it stop fine. However, as the elephant is most of the time not arrived at his destination, it keeps going to it and by the time it has and it reads the next instruction, it is not touching the cat sprite anymore and so the elephant keep moving.
I hope I make myself clear. Once the cat has been touched, I would like to pause the game and reset it to go to round 2.
- gwu777
-
New Scratcher
4 posts
Stop an infinite loop!
Don't make it do an infinite, I think you should try to do thisThank you. As said above, in this case the code doesn't stop if the end position of the gliding sprite is not directly on the cat. I cannot use this.forever
repeat until <touching [cat v] ?>
glide () secs to x: (pick random (0) to (10)) y: (pick random (0) to (10))
end
end
(Cat is the sprite name.)
- speckosaurus
-
Scratcher
100+ posts
Stop an infinite loop!
Don't use glide. You can make the elephant point in a random direction and then move it. This means its speed will be constant, since using glide means it will glide faster or slower depending in its distance from its random destination. This way it has a set speed, and now that you aren't telling it to move for a certain number of seconds each time, it won't keep moving past the cat sprite once it makes contact. Also the fact that the other examples have put a forever loop around the “repeat until” kind of defeats the purpose. Even when elephant has touched the cat, the forever loop will keep checking that condition forever (as the name implies), making it a little inefficient. Just have something like this:
Broadcast “start” whenever the game is ready to start or when you want to restart it
when I receive [start v]
repeat until <touching [cat v] ?>
point in direction (pick random (...) to (...))
move (10) steps
end
broadcast [caught v]
Broadcast “start” whenever the game is ready to start or when you want to restart it
Last edited by speckosaurus (Aug. 19, 2015 19:39:51)
- gor-dee
-
Scratcher
1000+ posts
Stop an infinite loop!
forever
point in direction ((pick random () to (10)) )
repeat (10)
move (1) steps
if <touching [cat] ?> then
stop [this script]
end
end
end
- gwu777
-
New Scratcher
4 posts
Stop an infinite loop!
Don't use glide. You can make the elephant point in a random direction and then move it. This means its speed will be constant, since using glide means it will glide faster or slower depending in its distance from its random destination. This way it has a set speed, and now that you aren't telling it to move for a certain number of seconds each time, it won't keep moving past the cat sprite once it makes contact. Also the fact that the other examples have put a forever loop around the “repeat until” kind of defeats the purpose. Even when elephant has touched the cat, the forever loop will keep checking that condition forever (as the name implies), making it a little inefficient. Just have something like this:when I receive [start v]
repeat until <touching [cat v] ?>
point in direction (pick random (...) to (...))
move (10) steps
end
broadcast [caught v]
Broadcast “start” whenever the game is ready to start or when you want to restart it
Thank you for your answers and trying to understand my problem which is a nice way to do what I needed. It is well thought and sure stop when needed, however, it hasn't got the nice feel of the glide and jumpp from one place to the other. I have to stop 0.3 second between each move to make it playable and it tend to stay in the same corner, even when making the steps number a random one.
It is a shame but at least I got the result I wanted.
- Sonsun_aviation
-
Scratcher
9 posts
Stop an infinite loop!
you can use this block if you want to stop your full script look at it
stop [ v]
- zahmbie1
-
Scratcher
1000+ posts
Stop an infinite loop!
you can use this block if you want to stop your full script look at itPlease do not nercopoststop [ v]
- Sonsun_aviation
-
Scratcher
9 posts
Stop an infinite loop!
use this code to stop the elephant
when green flag clicked
if <> then
<touching [ v] ?>
end
stop [ v]
- deck26
-
Scratcher
1000+ posts
Stop an infinite loop!
you can use this block if you want to stop your full script look at itWhy are you posting on a topic from 2015?stop [ v]
- Frankie_the_SDM
-
Scratcher
1 post
Stop an infinite loop!
you can use this block if you want to stop your full script look at itPlease do not nercopoststop [ v]
when green flag clicked
say [They we're just trying to help!]
- JIMMY_DA_GOOF
-
Scratcher
9 posts
Stop an infinite loop!
you can use this block if you want to stop your full script look at itPlease do not nercopoststop [ v]when green flag clicked
say [They we're just trying to help!]
when green flag clicked
say [yes]
Last edited by JIMMY_DA_GOOF (Feb. 7, 2021 18:29:16)
- Oumuamua
-
Scratcher
1000+ posts
Stop an infinite loop!
you can use this block if you want to stop your full script look at itPlease do not nercopoststop [ v]when green flag clicked
say [They we're just trying to help!]when green flag clickedsay [yes]

- Discussion Forums
- » Help with Scripts
-
» Stop an infinite loop!