Hi, I am trying to figure out how the draw loop works and having some trouble. I was going through some predictable mistakes the students would make and it isn’t performing the way I expected.
I would expect that only orange circles would be drawn on the screen with the above code. Once we enter the first draw loop I wouldn’t expect it to ever leave but instead…
It first draws a blue circle, so runs the blocks under the draw loop and the skips the first loop and goes into the 2nd loop. Is this a glitch? I am including the link incase anyone wants to play around with it also.
Thanks for your help!
https://studio.code.org/projects/gamelab/sEXXNO7T-R_MNw4ZyEujtr_OzLnjfyBiv7qWNzjYCBs
tldc: Redefining draw loop bad solution below
function draw() {
fill("orange");
ellipse(randomNumber(0,400), randomNumber(0,400));
fill("green");
ellipse(randomNumber(0,400), randomNumber(0,400));
}
Further Explanation Below
you’ve defined the draw loop twice, declaring it again overrides the initial declaration if you have learned about the concept of namespaces this is very much similar to the issue your having
Real World Comparison
david and joshua are currently in a waiting line for the deli line. the store accidentally gives 2 identical numbers but there is only 1 terminal available and neither joshua or david wishes to wait! this naming conflict forces the last defined instance of any datatype in a scope to be it’s current value hence if david goes first joshua would be #48 and not david and vice versa
Conclusion
the reason why the draw function loops is because of the way it’s set up in a try catch loop referencing your current function, if you wish to have both dots feel free to add it to the pre-existing draw loop
Varrience
I love the analogy @varrience! I’ve often wondered how to explain to my students there can only be one draw loop. If you add two, the first is skipped. Thanks!
~Michelle