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

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.

Mike

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.

1 Like