Side Scroller Game error

Whenever my students runs his game he comes up with the error code:

ERROR: Line: 67: TypeError: Cannot read properties of undefined (reading ‘toString’)

@langilla1 ,

Look at line 27. The health variable is being declared for a second time. It was already declared on line 19. Since one declaration was outside of the draw loop and the other one inside the draw loop, the program is getting confused between the two.

I’m guessing it was a mistake to declare it again on line 27. Perhaps they just meant to update it there (health = health - 1; instead of var health = health -1;

Let us know if that fixes the problem.


Line 66 takes care of adding “Health” as text on the top right side of the screen.
Line 67, also a text command, is unnecessary and should be removed.
The program will run after you do that, but there are further issues to look at.

That solved one problem but now when his soldier touches the enemy the enemy blurs.


That has something to do with the velocity of your background … I’m not sure why the background has a velocity, but if you remove that block near the beginning of the code, that should clear up the blurring issue.


I think you do need both lines 66 & 67. One displays the text “health” and the other displays the value of the variable “health.”

I looked at that first and realized that’s what they were doing… :slight_smile:

1 Like

Thank you so very much for your help!

Also, I got to thinking about the background velocity and I did find a way it could work if they want the background to move so there’s more motion…

It was a bit tricky and involves using two different background sprites that basically play leapfrog over each other. The 2nd one is also flipped so the tiling looks better since the left and right sides don’t match…

Anyway, here’s a remix with that code if anyone wants to look at it.

Scrolling Background


1 Like

Sorry, I should’ve double-checked before commenting; you do need lines 66 and 67.

As far as having moving backgrounds, more tweaking might be necessary. It looks like the backgrounds overlap —I can see two planets together at a time.

As for the functionality of the game, I think you might want to include your “soldier” in the Looping, that way he can continue to earn points by re-entering the screen from the left. You might also want to increase the up/down speeds.

One final note: the up/down directions are reversed y=y-1 goes up, y=y+1 goes down.