0

Im writing a webapp for a space invaders game and am getting this error when trying to write a function to get my Ship to shoot. "TypeError: can't define property "moveLaser": Object is not extensible" This is my code below.

Game.shoot = function(inv, invRem, sq, csi, w) {
let laserId;
let cli = csi;
Game.moveLaser = function() {
    sq[cli].classList.remove('laser');
    cli -= w;
    if (! 20 < cli < 421 ) { 
        sq[cli].classList.add('laser');
        if (sq[cli].classList.contains('invader')) {
            sq[cli].classList.remove('laser');
            sq[cli].classList.remove('invader');
            sq[cli].classList.add('collision');

            setTimeout(() => sq[cli].classList.remove('collision'), 300)
            clearInterval(laserId);

            let invaderRemoval = inv.indexOf(cli);
            invRem.push(invaderRemoval);
            results++;
            resultsDisplay.innerHTML = results;
        }
    } else {
        return;
    }
}
laserId = setInterval(Game.moveLaser(), 100);
return[inv, invRem, sq, csi, w];
Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • there is no reason to tag this as java – Stultuske Jun 16 '21 at 08:43
  • 1
    by the way `laserId = setInterval(Game.moveLaser(), 100);` will run `Game.moveLaser()` exactly once and once only – Jaromanda X Jun 16 '21 at 08:45
  • so, what is `Game` ... it's odd that you can extend it with `.shoot` but not with `.moveLaser` ... of course `moveLaser` will only be defined once you run `.shoot` ... do you have some code that runs `Object.preventExtensions(Game)` – Jaromanda X Jun 16 '21 at 08:46

0 Answers0