Sprite label can be "sprite" for multiple sprites

Link to the project or level: CSD Unit 3, Lessons 8, 9 & 10
What I expect to happen: When a student uses the label “sprite” for multiple sprites, I would expect only one sprite to appear.
What actually happens: All the sprites are drawn to the screen with a single draw sprites block.
CONCERN This makes it very confusing for students and they have a hard time understanding why they have to change the sprite label for each sprite. I wish game lab gave them an error (or warning) when they use a variable label again when defining a new variable.

Here is an example project: https://studio.code.org/projects/gamelab/f1hDXiOVjND8ikllSeRtx7BTGy3qAwuTYhTkyM_CH9U

1 Like

Hi @michelle_johnson. I’m not sure, but I’m guessing this is something you could also (unwisely) do in javascript itself, so it is allowed in GameLab. A warning would be nice for this kind of issue, but there are times when a variable could be reused (correctly) in a larger program. This would usually have to do with variables, some local and some global that aren’t accessed at the same time. Since a sprite is another variable (or closely related), it would be possible to have two sprites with the same name that didn’t conflict.

This is one of those things that would cause problems when the student tries to animate and/or otherwise control the sprites, so the would eventually see unintended consequences and have to figure it out. I try to teach them correctly from the beginning, but I do occasionally find errors such as this when they are further down the road and can’t get the rest of their code to work properly.


Reusing a label (variable name) is legal in JavaScript – and many other languages. What happens is that the later uses “hide” the earlier ones. Sometimes this is intentional, other times it is not.

I think what happens when you do something like this:

var sprite = createSprite(100, 100);
var sprite = createSprite(200, 200);

Is that the sprite objects are stored in an array or list (to facilitate processing by drawSprites()) and a reference to the sprite object is returned and stored in a variable called sprite – since the second call to createSprite() also stores the reference in a new variable called sprite which replaces the first reference since they have the same name.

The result is that the list of sprites has two sprites in it, but we only know how to find and refer to the last one. So drawSprites() still draws both sprites, but we only have a. way to refer to the last one.

It’s kind of like having one bank account, and then setting up a new one – but instead of adding a new entry in your address book of the second bank account you replace the information in your “bank account” entry with the details of your second account. You still have both bank accounts, but you only know how to get to the second one. The first one is effectively lost to you – at least until they send you a statement :slight_smile:

This is why I lean heavily on my students to always give their sprites meaningful names. To the point that I add it to the description of my rubrics.


This makes sense so thank you for explaining. Everything is just more challenging while teaching all virtual and on Zoom. My students finally understood renaming their sprites when we got to the animations lesson.

I hear you there. The virtual teaching isn’t ideal, but I still feel like my students are learning, perhaps just at a bit slower of a pace.


1 Like

Teaching online is so much more challenging (for me at least), but it’s also an opportunity. Some kids seem clearly happier and more confident in a “less personal” learning space – that has been eye opening for me.

Also, this feels like an opportunity to teach “real world” skills for independent learning and remote collaboration. I think even if we’re not teaching those skills, we’re forcing the kids to learn them – they may actually be learning at a faster pace (but covering a broader “curriculum”).

One thing that I’ve found helpful is to use the “participants select own breakout rooms” feature is Zoom, when you do that the kids can “move around” the classroom and confer with each other as they need to. You can use that feature and still assign the groups manually so that the kids go into a default group.

1 Like

Well, all the sprites are kept in World.allSprites, so if you want to use that just run World.allSprites.
The reason for why all the sprites are getting drawn is that the SPRITE is getting created…
All you’re doing with the variable is assign the sprite to it…
think about it this way… The sprite is an OBJECT. By running drawSprite() you draw all the
SPRITE OBJECTS. Sorry for the late response…