Unit 3 - Sprites and When to Create Them Vs. Draw Loop

I am looking at unit 3’s second mini project, lesson 13. I have a pretty green question. Everything in code.org shows that sprites are created before the draw loop, then updated in the draw loop. Students have created the sprites after the draw loop, though, and their program still runs as expected. Should sprites be created before or after the draw loop, or does it matter?

@walkerak,

They should be created outside of the draw loop. Most of the time, the advice is to create them before the draw loop (with all other variables). However, it may not matter.

Creating them inside the draw loop does cause problems though and that should be avoided.

I personally like to put all of my variables (including sprites which are technically variables) at the top of the code and all of my functions at the bottom of my code.

If you want a more technical explanation, this link about “hoisting in javascript” should help.

Hope this helps!

Mike

2 Likes