Shooting bullets class -- bullets disappearing

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?

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() {
  //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);

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 {

Best of luck.

1 Like

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

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.

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).