Discuss Scratch
- Discussion Forums
- » Bugs and Glitches
- » Cloning in a loop doesn't work in my case
- Michel75
-
Scratcher
2 posts
Cloning in a loop doesn't work in my case
Hi everybody, and thanks for this wonderful tool :-)
I made a test project (an asteroid clone) here and i have a problem :
when the asteroid clone itself to make smaller asteroids, I can't make a repeat loop.
(in object astero-cookie)
This work well :

But this is not working :

I guess there is a side effect, but i don't know why…
If somebody could help…
Thanks & sorry for my poor english…
I made a test project (an asteroid clone) here and i have a problem :
when the asteroid clone itself to make smaller asteroids, I can't make a repeat loop.
(in object astero-cookie)
This work well :

But this is not working :

I guess there is a side effect, but i don't know why…
If somebody could help…
Thanks & sorry for my poor english…
- DadOfMrLog
-
Scratcher
1000+ posts
Cloning in a loop doesn't work in my case
The problem is the way that Scratch does loops and broadcasts.
I hope this is reasonably easy for you to follow - and I apologise in advance for my very poor French…
First of all, loops…
Scratch has a kind of ‘timer’ which ‘fires’ lots of times a second. Each time it ‘fires’ the timer, it goes to every loop in every script that makes some change to what appears on the screen (roughly speaking), and does one iteration of each loop.
Now, for your “repeat (2)” when creating clones, the first repeat works fine - it creates a clone. Then, before it does the second time through the loop, it starts to do another iteration of every loop in all the scripts - in particular, it does the next iteration of the “repeat until” loop in your Stage's “Niveau” custom block, which means it does another broadcast of “bouger” (remember that your astero-cookie is still in the middle of its script after receiving the broadcast earlier).
OK, secondly, broadcasts…
If a sprite receives a broadcast then it starts the script that goes with it. If it receives the broadcast again while that script is still going, it *stops the running script* and starts it again.
Right, so put the together the loops and broadcasts…
What happens is that your first clone gets created in the “repeat (2)” loop. It then doesn't do the second repeat, but instead goes back through other repeats in other scripts - where the “repeat until” of the “Niveau” block does another “bouger” broadcast. This stops the script in your “astero-cookie” sprite that just created the clone, and starts it again - so it never gets around to doing the second repeat to create the second clone, and it never gets as far as deleting the current clone (the big astero-cookie).
A simple way around the problem would be to "broadcast bouger and wait' in the repeat until loop - you should probably do that anyway, since I expect you don't want it to resend the broadcast until all the astero-cookies have already done everything you want them to (otherwise they will start trying to move again before they have finished moving the first time).
Hope that makes some sense…
I hope this is reasonably easy for you to follow - and I apologise in advance for my very poor French…

First of all, loops…
Scratch has a kind of ‘timer’ which ‘fires’ lots of times a second. Each time it ‘fires’ the timer, it goes to every loop in every script that makes some change to what appears on the screen (roughly speaking), and does one iteration of each loop.
Now, for your “repeat (2)” when creating clones, the first repeat works fine - it creates a clone. Then, before it does the second time through the loop, it starts to do another iteration of every loop in all the scripts - in particular, it does the next iteration of the “repeat until” loop in your Stage's “Niveau” custom block, which means it does another broadcast of “bouger” (remember that your astero-cookie is still in the middle of its script after receiving the broadcast earlier).
OK, secondly, broadcasts…
If a sprite receives a broadcast then it starts the script that goes with it. If it receives the broadcast again while that script is still going, it *stops the running script* and starts it again.
Right, so put the together the loops and broadcasts…
What happens is that your first clone gets created in the “repeat (2)” loop. It then doesn't do the second repeat, but instead goes back through other repeats in other scripts - where the “repeat until” of the “Niveau” block does another “bouger” broadcast. This stops the script in your “astero-cookie” sprite that just created the clone, and starts it again - so it never gets around to doing the second repeat to create the second clone, and it never gets as far as deleting the current clone (the big astero-cookie).
A simple way around the problem would be to "broadcast bouger and wait' in the repeat until loop - you should probably do that anyway, since I expect you don't want it to resend the broadcast until all the astero-cookies have already done everything you want them to (otherwise they will start trying to move again before they have finished moving the first time).
Hope that makes some sense…
Last edited by DadOfMrLog (May 23, 2013 16:35:42)
- BoltBait
-
Scratcher
1000+ posts
Cloning in a loop doesn't work in my case
But this is not working :
I guess there is a side effect, but i don't know why…
If somebody could help…
Before or after the “create clone of myself” block, put a “wait (0.1)” block. See if that works.
- Michel75
-
Scratcher
2 posts
Cloning in a loop doesn't work in my case
Thanks for this explanation ! it works now 
I red that the source code of Scratch v2.0 will be available this summer, is it still true ? i'd really like to have a look
PS : French was flawless

I red that the source code of Scratch v2.0 will be available this summer, is it still true ? i'd really like to have a look
PS : French was flawless

- Discussion Forums
- » Bugs and Glitches
-
» Cloning in a loop doesn't work in my case


