Code Problems, Please Help!


#1

My students are having trouble with their code and I cannot figure out their issue. They are getting a

ERROR: Line: 188: TypeError: Cannot read property ‘copy’ of null

I am not sure what what is Null as it looks like enemy2 Sprite has been instantiated.

Any help would be greatly appreciated. here is the code:


#2

Interesting… I haven’t figured out what’s wrong here, but I have some clues that may point you in the right direction.

First, I commented the problem code out. I set the Watch area beside the console to monitor enemy.x, enemy.y, enemy2.x and enemy2.y.

The values for enemy.x and enemy.y reported values I would expect, but the enemy2 values were both NaN. After I moved the character off to the right (increasing the loop variable), enemy2.x suddenly had a value but enemy2.y still didn’t. I’m not sure why.
I added enemy2.x = 200; and enemy2.y = 200; to function background1(), and things started working in a much more expected way. The only problem was that the bee was now stuck in that position.

Sorry that I wasn’t able to discover anything else. Hopefully that gets you started!
Mike


#3

@mike
on line 41, there is no property .velocity. When I changed it to .velocityX, the program works.

Andrea


#4

Nice catch, Andrea! I had a feeling it was something like that.


#5

Hey @anmrobnott and @mike thanks for jumping in and helping. We’d shared this with engineering who thought the bug might be related to recent backend tweaks in Game Lab. It turns out that this bug is a little more subtle. Sprites have a hidden velocity property that actually contains information about both the X and Y velocity. When it’s set to a single value in Line 41 this code breaks the underlying structure of a sprite that the rest of the code is relying on.

It seems like this kind of mistake is likely to be a common one so we’ve shared it with engineering to see if we can come up with a way to warn users about this error. As we saw here, even with several eyes on it this bug can take a while to catch.

Cheers!
GT


#6

Awesome, Thanks for the help everyone. This was two of my higher level students so it’s a surprise they didn’t catch it…