Unit 3: Lesson 20 Problem with Code

Wow, this was a really interesting bug. I noticed it only happened after the user lost, so I figured it was probably being caused by something in the “loser” function. It looks like what’s happening is that inside the “loser” function, the velocity property of each sprite is getting set to 0. But it should probably be the “velocityX” property.

Long explanation:
The “velocity” property (which is normally hidden to us) holds some other information about the sprite, including a function called “magSq”. That function gets called when you use the displace method. When the student set the “velocity” property to 0, it overwrote the function that the displace method needed to use. So, when the draw loop went back and called displace, it couldn’t find the function that it needed, so it threw an error.

Elizabeth