I am making a game where a ship fires missiles at aliens. Each time the player presses space, a new missile appears, but the old one disappears, so the player can only have one missile on the screen at a time.
My code is:
var missile = createSprite(-200, -200);
missile.setAnimation(“missle”);
missile.scale = 0.5;
missile.velocityY = -10;
if (missiles>0) {
if (keyWentDown(“space”)) {
x = ship.x;
y = ship.y;
missile.x = x;
missile.y = y;
missiles = missiles-1;
}
I am trying to write code where each time space is pressed, a new missile appears, but the old one doesn’t disappear. It isn’t something that was taught in any of the lessons, and I can’t seem to find a solution that works. Any suggestions would be greatly appreciated!
It’s a tiny bit hard to diagnose without having access to your code, BUT … you likely do only have one sprite at a time, but you can still see the “ghost” of the old sprite unless you have a background block in your draw Loop to cover up old instances of the sprite.
If you want to share the link, we could maybe help diagnose a little better.
OK. I think I understand a little better now. You only created a single variable denoting a single missile. If you want to have more missiles, you will want to create a group of missiles and create a new instance of the missile when firing.
Here’s a link to the documentation on groups so you can perhaps figure out how that might be implemented: