Code Problems, Please Help!

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:

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

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

Andrea

2 Likes

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

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

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

Hi - we are in the lesson prior to introducing velocity. I’m searching, but cannot find what code velocityX and velocityY are actually invoking. I would love to be able to show my students how to do this without using velocity before introducing it.
Thanks!! :slight_smile:

The short answer is that they are turning the counter pattern into a function so you don’t have to invoke it continuously in the draw loop. This is discussed a little in unit 3 lesson 15, bubbles 2 (teacher’s note) and 3, but it isn’t taught in great detail. However, if you teach them the counter pattern (lessons 8 & 9 of unit 3), using the counter pattern to change the x & y values of a sprite is the same thing as velocity. Velocity is just easier to invoke.

Hope this helps a little, but if it’s as clear as mud, let us know and we will try again!

Mike

1 Like

Hi Michael,

This is the student’s project:

https://studio.code.org/projects/gamelab/xc_SMqH2D5Fp1WEI5fVugbC4k01VpakQnrgo6tKxBRE

He is wanting the bunny to continue to move when the left mouse button is clicked once.

I’ve tried changing the pattern around several different ways, but cannot get the bunny to move continuously with one click.

Hi. Thanks for the link. My suggestion is to have two conditionals in your draw loop. The first conditional will be triggered by mouse down (as it is now), but instead of putting the bunny’s motion in the first conditional, have the first conditional change a variable from false to true. (You will have to create that variable at the top of your program). The 2nd conditional will check to see if the variable is true and if it is, it will move the bunny. Then, below both conditionals, I would have it draw the sprites. So generally, it checks to see if the mouse was clicked and if not, it skips to the 2nd conditional and the variable will still be false, so it will go back to the first and check the mouse. Once the mouse has clicked, the variable will be true and the 2nd conditional loop will continuously run and the bunny will move.

There is probably another way, but that’s the easiest way I can think of to make it work.

Give it a shot and let us know how it goes.

Mike

3 Likes

This worked great! Thanks so much!

2 Likes