Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Stop an infinite loop!
- gwu777
-
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?
- speckosaurus
-
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
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
-
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.)
(Cat is the sprite name.)
- Crimson19
-
1000+ posts
Stop an infinite loop!
Last edited by Crimson19 (Aug. 19, 2015 14:28:25)
- gwu777
-
4 posts
Stop an infinite loop!
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
-
4 posts
Stop an infinite loop!
Thank 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. Don't make it do an infinite, I think you should try to do this
(Cat is the sprite name.)
- speckosaurus
-
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
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)
- gwu777
-
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:
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
-
9 posts
Stop an infinite loop!
you can use this block if you want to stop your full script look at it
- zahmbie1
-
1000+ posts
Stop an infinite loop!
Please do not nercopost you can use this block if you want to stop your full script look at it
- deck26
-
1000+ posts
Stop an infinite loop!
Why are you posting on a topic from 2015? you can use this block if you want to stop your full script look at it
- Frankie_the_SDM
-
1 post
Stop an infinite loop!
Please do not nercopost you can use this block if you want to stop your full script look at it
- JIMMY_DA_GOOF
-
9 posts
Stop an infinite loop!
Please do not nercopost you can use this block if you want to stop your full script look at it
Last edited by JIMMY_DA_GOOF (Feb. 7, 2021 18:29:16)
- Oumuamua
-
1000+ posts
Stop an infinite loop!
Please do not nercopost you can use this block if you want to stop your full script look at it

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