Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: can't access property "classList" of undefined app.js:107:5 #3

Open
Fishbite opened this issue Jun 3, 2021 · 3 comments

Comments

@Fishbite
Copy link

Fishbite commented Jun 3, 2021

Hey Ania,

This is the error running your unadulterated code with the exception that I corrected the link to the css file in the html file.

Uncaught TypeError: can't access property "classList", squares[currentLaserIndex] is undefined
moveLaser https://fishbite.github.io/space-invaders/app.js:107
setInterval handlershoot https://fishbite.github.io/space-invaders/app.js:130
EventListener.handleEvent
https://fishbite.github.io/space-invaders/app.js:134
app.js:107:5

It occurs when the laser shot exits the top of the screen and I'm not sure how to fix it!!! Any ideas?

Thanks for the video tutorial BTW. It's much appreciated by many folks out there especially myself :)

@CoderCH62
Copy link

Insert this code right at the top of the moveLaser function:

    if (currentLaserIndex < width){
      squares[currentLaserIndex].classList.remove('laser')
      clearInterval(laserId)
      return
    }

@Fishbite
Copy link
Author

That's similar to my own fix:

if (currentLaserIndex - width < 0) {
            squares[currentLaserIndex].classList.remove("laser");
            return;

which seems to have cured the problem. I didn't use clearInterval() though. Interesting!?
https://fishbite.github.io/space_invaders/

@CoderCH62
Copy link

You should yet clear the Interval of laserID, otherwise every laser beam exist forever and it's index value will continue decreasing every interval. It will probably never result in a problem for this little game, however, is not good practice to keep the interval alive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants