Unit 3: Lesson 20 Problem with Code


My student keeps getting an error message on line 27 “velocity.Magsq” is not a function. What does this mean? How do we get it to stop? He wants to allow the alien to walk after the “you lose” he doesn’t want the game to freeze. He wants it to start again. How do we fix it?


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.