# Shooting bullets class -- bullets disappearing

**URL:** <https://discourse.processing.org/t/shooting-bullets-class-bullets-disappearing/4028>\
**Category:** Coding Questions\
**Created:** [October 1, 2018, 9:46pm UTC](https://discourse.processing.org/t/shooting-bullets-class-bullets-disappearing/4028 "2018-10-01T21:46:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcus152](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/marcus152/32/1798_2.png) [@Marcus152](https://discourse.processing.org/u/Marcus152)\
**Post date:** [October 1, 2018, 9:46pm UTC](https://discourse.processing.org/t/shooting-bullets-class-bullets-disappearing/4028/1 "2018-10-01T21:46:09Z")

</div>

Hey guys, i’m trying to make a game where bullets shoot in the direction of my mouse. I found some code online, and it worked for the most part; except for one thing. The original code that I found used a 600x600 window size, I changed it to be an 800x600 as that was the size of my game. However when I did so my bullets started disappearing 200 pixels away from the edge of the window on the right side (if that makes sense). I tried a few things to bypass this and make the bullets disappear at 800 instead of 600 and no luck. Can anybody take a look at my code and help me out?

```auto
//Objects
Player player = new Player();
//Bullet arraylist
ArrayList<Bullet> bullets;
//Vars to regulate shooting speed
boolean canShoot = true;
float canShootCounter;

int i;

void setup() {
  size(800, 600);
  //standard array setup
  bullets = new ArrayList<Bullet>();
  player = new Player();
}

void draw() {
  player.update();
  background(100);
  //since we are adding and removing objects so often, we need to do the forloop backwards preventing any nullPointer errors
  for (i = bullets.size()-1; i >= 0; i--) {
    //you need a seperate var to get the object from the bullets arraylist then use that variable to call the functions
    Bullet bullet = bullets.get(i);
    bullet.update();
  }
}

class Player {
  PVector location;
  Player() {
    location = new PVector(width/2, height/2);
  } 
  void update() {
    if (mousePressed == true) {
      // this regulates the shooting speed
      if (canShoot == true) {
        bullets.add( new Bullet());
        canShoot = false;
        canShootCounter = 0;
      }
    }
    // this checks if the right amount of time has passed before canShoot can = true again
    if (canShoot == false) {
      canShootCounter ++;
      //if the right amount of time has passed. make canShoot true
      if (canShootCounter == 5)/*change this number to change the duration*/{
        canShoot = true;
      }
    }
  }
}

class Bullet {
  //standard PVector used for the location of the bullet
  PVector location;
  //vars used to check the angle between location and the mouse
  float oldPosX, oldPosY, rotation, speed;
  Bullet() {
    //places the bullet in the middle of the room
    location= new PVector(width/2, height/2);
    //this checks the angle
    oldPosX = mouseX;
    oldPosY = mouseY;
    rotation = atan2(oldPosY - location.y, oldPosX - location.x) / PI * 180;
    //bullet speed
    speed = 10;//change this number to change the speed
  } 
  void update() {
    //move the bullet
    location.x = location.x + cos(rotation/180*PI)*speed;
    location.y = location.y + sin(rotation/180*PI)*speed;
    ellipse(location.x, location.y, 10, 10);

    //removes the bullet from the arrayList if it is off the room
    if (location.x > 0 && location.x < width && location.y > 0 && location.x < height) {
    }
    else {
      bullets.remove(i);
    }
  }
}

```

Best of luck.  
-Marcus

---

<div class="post-metadata">

**Author:** ![Marcus152](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/marcus152/32/1798_2.png) [@Marcus152](https://discourse.processing.org/u/Marcus152)\
**Post date:** [October 1, 2018, 10:45pm UTC](https://discourse.processing.org/t/shooting-bullets-class-bullets-disappearing/4028/2 "2018-10-01T22:45:24Z")

</div>

Alright, found out the x length was being set to the height which would work if it was a square, but it’s (obviously) not. Thanks

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 3, 2018, 1:42am UTC](https://discourse.processing.org/t/shooting-bullets-class-bullets-disappearing/4028/3 "2018-10-03T01:42:11Z")

</div>

Glad you solved your problem!

It is a common problem when changing square sketches to have to carefully check all uses of dimensions and make sure they are separate.

> [@Marcus152](#):
>
> if (location.x \> 0 && location.x \< width && location.y \> 0 && location.x \< height) {

However that last `location.x < height` looks like it is just an error in the original code. If something headed straight down, it would never be removed – even at `(50, 10000000)`.
