# Fix code to make spaceship blaster shoot

**URL:** <https://discourse.processing.org/t/fix-code-to-make-spaceship-blaster-shoot/789>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [June 8, 2018, 12:20am UTC](https://discourse.processing.org/t/fix-code-to-make-spaceship-blaster-shoot/789 "2018-06-08T00:20:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![the-junk](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@the-junk](https://discourse.processing.org/u/the-junk)\
**Post date:** [June 8, 2018, 12:20am UTC](https://discourse.processing.org/t/fix-code-to-make-spaceship-blaster-shoot/789/1 "2018-06-08T00:20:40Z")

</div>

Why won’t my code work to make my spaceship blaster shoot?

```
float shipX; // player spaceship
float shipY; // player spaceship 
float enemyX; //enemy UFO
float enemyY; //enemy UFO
float enemySpeed = 2; 
float blastX;
float blastY;

void setup() {
  size (500, 500);
}

void drawSpaceship(float shipX, float shipY) {
  fill (228, 232, 26);
  triangle(shipX, shipY+475, shipX+25, shipY+450, shipX+50, shipY+475);
}
void drawEnemy(float enemyX, float enemyY) {
  rectMode(CENTER);
  ellipse(enemyX, enemyY+250, 30, 20);
  rect(enemyX, enemyY+250, 50, 10);
  enemyX += 1;
}
void moveEnemy() {
  fill (random(200), 0, 0);
  enemyX += enemySpeed;
  if (enemyX > width || enemyX < 0) {
    enemySpeed *= -1;
  }
}
void shootBlaster(float blastX, float blastY) {
  fill(0, 0, 200);
  triangle (blastX, blastY+495, blastX+5, blastY+490, blastX+10, blastY+495);
  if (mousePressed) {
    blastY += -5;
  } // why is this if condition not causing blastY to move? What am I missing?
}

void draw() {
  background(200);
  moveEnemy();
  drawEnemy(enemyX, enemyY); // enemy UFO
  drawSpaceship(mouseX, shipY); // player spaceship at canvas bottom
  shootBlaster(mouseX+20, blastY-20); //this code should shoot a triangle up the canvas
}
```

---

<div class="post-metadata">

**Author:** ![Grimtin10](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Grimtin10](https://discourse.processing.org/u/Grimtin10)\
**Post date:** [June 8, 2018, 2:03am UTC](https://discourse.processing.org/t/fix-code-to-make-spaceship-blaster-shoot/789/2 "2018-06-08T02:03:50Z")

</div>

You never initialize the variables. Try setting them to a default value.

---

<div class="post-metadata">

**Author:** ![dan850](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dan850](https://discourse.processing.org/u/dan850)\
**Post date:** [June 8, 2018, 8:11am UTC](https://discourse.processing.org/t/fix-code-to-make-spaceship-blaster-shoot/789/3 "2018-06-08T08:11:08Z")

</div>

hi @the-junk, also i don’t see any functions that draw the spaceship shooting or the enemy being destroyed. so for example, i would create another function like `destroyEnemy(enemyX, enemyY)` with other shapes illustrating destruction of the ship, then test out the logic.🙂 Unless this is not all your code. best of luck

---

<div class="post-metadata">

**Author:** ![colin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/colin/32/230_2.png) [@colin](https://discourse.processing.org/u/colin)\
**Post date:** [June 8, 2018, 8:17am UTC](https://discourse.processing.org/t/fix-code-to-make-spaceship-blaster-shoot/789/4 "2018-06-08T08:17:46Z")

</div>

Hi,

The answer is pretty simple. While your vertical value is stored in your public variable blastY, it is still initialized to 0 each frame when your drawSpaceship() function is called because it has a parameter variable with the exact same name. You should only use blastY as a public variable and blastX as a private one.

Just replacing your function by this one will make it work.

```auto
void shootBlaster(float x, float y) {
  fill(0, 0, 200);
  triangle (x, blastY+495, x+5, blastY+490, x+10, blastY+495);
  if (mousePressed) blastY += -5;
}

```
