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.