Debugging: Laser Shoot doesn't move with spaceship

[Game Lab - Code.org]

What I expect to happen: The red laser should shoot from spaceship where ever spaceship is when player hits button to shoot

What actually happens: The laser is stuck in the middle of the screen and does not look like it is coming from spaceship and does not follow the spaceship.

What I’ve tried: Student has tried several revisions to the var laser. One such revision can be found here: [Game Lab - Code.org]

Many thanks for any help you can give this student. I am always thankful to all of you for your help when I can’t help a student!

~Sheri

hello @sheri.mcnair

the way your doing it is on the right track, however whenever you create a sprite like that it will be added in and will be drawn vs as a placeholder

var laser = [];
var j = 0;
// for (var i = 0; i < 9; i++) {
//   laser[i] = createSprite(200,200);
//   laser[i].setAnimation("laser");
// }

function movement() {
  if (keyDown("left")) {
    spaceShip.velocityX = -10;
  } else {
    spaceShip.velocityX = 0;
  }
  if (keyDown("right")) {
    spaceShip.velocityX = 10;
  }

  if (keyWentDown(UP_ARROW) && laser.length < 10) {
    var beam = createSprite(spaceShip.x, spaceShip.y - 20);
    beam.setAnimation("laser");
    beam.velocityY = -10;
    laser.push(beam);
    // laser[j].velocityY = -10;
    // laser[j].X = spaceShip.X;
    // laser[j].Y = 200;
    // j = j + 1;
    // if (j > 9) {
    //   j = 0;
    // }
  }
  for(var i = 0; i < laser.length; i++) {
   var beam = laser[i];
   if(beam.y < 0) {
    beam.destroy();
    laser.splice(i, 1);
    i -= 1;
   } 
  }

doing something like this should fix it setting an ammo cap just like you wanted as well and removing items from it later when it’s no longer on screen (you can make it different behavior if you want just simpler and less lines) PS also you have 2 laser shooting functions in the code i was editing so put the function where you want… just not the best idea to have 2 of them or the old firing mechanism either hope this helps

Varrience

Thank you so much for taking the time to look at the code and come up with a solution to the issue! I am sure I will have a very happy student on Monday when I explain what to do. Enjoy your weekend and thank you for always helping us out!