Discuss Scratch

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?

foreverglidesecstox:pickrandom0to10y:pickrandom0to10
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:
repeatuntiltouchingcat?

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

foreverrepeatuntiltouchingcat?glidesecstox:pickrandom0to10y:pickrandom0to10

(Cat is the sprite name.)
Crimson19
Scratcher
1000+ posts

Stop an infinite loop!

foreverglide1secstox:...y:...iftouchingcatstopthis script

Last edited by Crimson19 (Aug. 19, 2015 14:28:25)

gwu777
New Scratcher
4 posts

Stop an infinite loop!

Crimson19 wrote:

foreverglide1secstox:...y:...iftouchingcatstopthis script

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!

Zekrom01 wrote:

Don't make it do an infinite, I think you should try to do this

foreverrepeatuntiltouchingcat?glidesecstox:pickrandom0to10y:pickrandom0to10

(Cat is the sprite name.)
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.
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:

whenIreceivestartrepeatuntiltouchingcat?pointindirectionpickrandom...to...move10stepsbroadcastcaught

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!

foreverpointindirectionpickrandomto10repeat10move1stepsiftouchingcat?thenstopthis script
gwu777
New Scratcher
4 posts

Stop an infinite loop!

speckosaurus wrote:

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:

whenIreceivestartrepeatuntiltouchingcat?pointindirectionpickrandom...to...move10stepsbroadcastcaught

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
zahmbie1
Scratcher
1000+ posts

Stop an infinite loop!

Sonsun_aviation wrote:

you can use this block if you want to stop your full script look at it
stop
Please do not nercopost
Sonsun_aviation
Scratcher
9 posts

Stop an infinite loop!

use this code to stop the elephant
whenclickedifthentouching?end
stop
deck26
Scratcher
1000+ posts

Stop an infinite loop!

Sonsun_aviation wrote:

you can use this block if you want to stop your full script look at it
stop
Why are you posting on a topic from 2015?
Frankie_the_SDM
Scratcher
1 post

Stop an infinite loop!

zahmbie1 wrote:

Sonsun_aviation wrote:

you can use this block if you want to stop your full script look at it
stop
Please do not nercopost
whenclickedsayThey we're just trying to help!
JIMMY_DA_GOOF
Scratcher
9 posts

Stop an infinite loop!

Frankie_the_SDM wrote:

zahmbie1 wrote:

Sonsun_aviation wrote:

you can use this block if you want to stop your full script look at it
stop
Please do not nercopost
whenclickedsayThey we're just trying to help!
whenclicked
sayyes

Last edited by JIMMY_DA_GOOF (Feb. 7, 2021 18:29:16)

Oumuamua
Scratcher
1000+ posts

Stop an infinite loop!

JIMMY_DA_GOOF wrote:

Frankie_the_SDM wrote:

zahmbie1 wrote:

Sonsun_aviation wrote:

you can use this block if you want to stop your full script look at it
stop
Please do not nercopost
whenclickedsayThey we're just trying to help!
whenclicked
sayyes

Powered by DjangoBB