CSD U3 L22 C4 Code Not Working as Expected

csd-unit-3
#1

My student is working on a game that isn’t doing what he expects:

He’s got a castle with a door in it, and when he uses the arrow keys to move the sprite to the door, the scene changes to a new scene. In the new scene, there is a new sprite that has to avoid dodgeballs hitting it until a timer reaches 1000.

But instead of moving to the new scene, the screen goes completely grey.

Errors: Around lines 106-7, a message appears saying that the set.Animation is not a function OR that there is an overlap.

No matter what the student does, he can’t get the code to run as he intended.

Any thoughts?

Thank you!

Gary Ellis
Teacher

#2

Gary,

The link above says the project isn’t available for sharing - might need to head into your Teacher dashboard and turn that feature on? Either way - post the link again and we’ll work together to figure it out!

Brad

#3

Thanks, Brad. That’s weird. I clicked on Share and then copied the link that appeared:

https://studio.code.org/projects/gamelab/-CmxNeHQIPwDFXEvCJZTSmaL6ng8oh_0UmxQBC0WJK4

Gary

#5

Yikes! @misterellis, it’s saying the same thing on the new link.

I’m going to ask our team about this

best,
kevin

#6

Hi Kevin:

I could take a pic of the code and send that, if it would help.

Gary

#7

Hi Brad/Kevin:

Do you have any thoughts about the problem my student is facing?

Thanks!

Gary

#8

In order for us to help out, you’ll need to turn on sharing for the class. You can do that by following the instructions in this help article: https://support.code.org/hc/en-us/articles/115001554911-Configuring-sharing-options-for-students-using-App-Lab-Game-Lab-and-Web-Lab

Otherwise, we will not be able to play with the code.

Alternatively, you can choose to “Remix” the student code into your own account and share the version from your own account.

Thanks,
Elizabeth

#9

Done—thank you very much!

Gary

#10

Here’s how I went about debugging this code:

That error about S1K.setAnimation not being a function is strange because sprite methods such as setAnimation are always automatically functions. That means that one of two things has happened. Either S1K.setAnimation has been redefined to something else (not the function it should be), or S1K has been redefined to something that is not a sprite, and so doesn’t have the method setAnimation attached to it.

I searched the entire program for all references to S1K to see if I could find where it or its setAnimation method was redefined. (I needed to be in text mode for this.)

On line 70, I found that S1K was being set to a number. This is where the bug actually is.

Some background on this type of bug:
At the beginning of the program, the sprite was created, then stored into a variable called “S1K”. From then on, the student is able to use the “S1K” variable to access and manipulate the sprite. On line 70, the student accidentally assigned the number 150 to the “S1K” variable. After that, the sprite still existed and was drawn to the screen, but the student could no longer use the “S1K” variable to access and manipulate it. The program didn’t have a problem yet, though, because the student wasn’t trying to use the variable to access the sprite.

In line 127, the student tried to use the “S1K” variable to access the sprite and run the sprite’s setAnimation method to change its animation. When the program accessed the “S1K” variable, instead of the sprite, there was the number 150 inside. The number 150 does not have a setAnimation method, so the program threw an error.

Elizabeth

1 Like