how to delete object

is it possible to delete an object, for example if something gets shot? if so how do i do so. i ask because i'm making a shooter, but it has a huge memory leak.

my code

color c = color(255);
float intX = 150;
int intY = 375;
float shipSpeed = 2;
int A = 0;
int B = 0;
float r;
float enemy1X;
float yEnemy2;
float yEnemy3;
int score = 0;
int lives = 5;
float FR = 0;
int highscore = 0;
float money = 0;
boolean pause = false;
boolean upScreen = false;
int oneClick = 0;

ArrayList<Bullet> bullets;
ArrayList<Enemy> enemies;
ArrayList<Enemy2> enemies2;
ArrayList<Enemy3> enemies3;
ArrayList<Upgrade> upgrades;

void setup() {
  size(300, 400);
  bullets = new ArrayList();
  enemies = new ArrayList();
  enemies2 = new ArrayList();
  enemies3  = new ArrayList();
  upgrades = new ArrayList();
  upgrades.add(new Upgrade(100, 100, 1, 4));
}

void draw() {
  if (upScreen == false) {
    if (pause == false) {
      if (lives > 0) {
        FR = 60 + score*0.1;
        frameRate(FR);
        background(0);
        pause();
        move();
        onScreen();
        spawn();
        display();
        for (Bullet bullet : bullets) bullet.draw();
        for (Enemy enemy : enemies) enemy.draw();
        for (Enemy2 enemy2 : enemies2) enemy2.draw();
        for (Enemy3 enemy3 : enemies3) enemy3.draw();
        A = A-1;
        if (score>highscore) {
          highscore = score;
        }
      } else {
        if (B == 0) {
          fill(0);
          rect(0, 0, 200, 100);
          display();
          B = 1;
        }
        display2();
        if (mousePressed == true) {
          lives = 5;
          score = 0;
          for (Enemy enemy : enemies) enemy.shot = true;
          for (Bullet bullet : bullets) bullet.xBullet = -150;
          for (Enemy2 enemy2 : enemies2) enemy2.shot = true;
          for (Enemy3 enemy3 : enemies3) enemy3.shot = true;
          intX = 150;
        }
      }
    } else {
      pause();
    }
  } 
  if (upScreen == true) {
    upgrades();
    for (Upgrade upgrade : upgrades) upgrade.draw();
  }
}

void move() {
  if (keyPressed) {
    if (key == 'd' || key == 'D') {
      intX = intX+shipSpeed;
    }
    if (key =='a' || key == 'A') {
      intX = intX-shipSpeed;
    }
  }
}

void display() {
  rectMode(CENTER);
  noStroke();
  fill(c);
  textSize(12);
  rect(intX, intY, 10, 20);
  textAlign(LEFT);
  text("score:", 0, 10);
  text(score, 45, 10);
  text("lives:", 0, 50);
  text(lives, 45, 50);
  text("highscore:", 0, 30);
  text(highscore, 60, 30);
  text("money:", 0, 70);
  text(String.format("%.0f", money), 45, 70);
}

void onScreen() {
  if (intX >= width-5) {
    intX = width - 6;
  }
  if (intX <= 5) {
    intX = 6;
  }
}

void spawn() {
  r = random(1, 100);
  if (r >= 98 && r <= 99) {
    enemy1X = random(15, width-15);
    enemies.add( new Enemy(enemy1X, color(0, 0, 255)));
  }
  if (score >= 5) {
    if (r >= 97.75 && r<= 98) {
      yEnemy2 = random(10, height/2);
      enemies2.add( new Enemy2(yEnemy2, color(0, 255, 0)));
    }
    if (score >= 20) {
      if (r >= 50 && r<= 50.25) {
        yEnemy3 = random(10, height/2);
        enemies3.add( new Enemy3(yEnemy3, color(255, 0, 0)));
      }
    }
  }
}

void keyPressed() {
  if (A <= 0) {
    if (key == 'w' || key == 'W') {
      bullets.add( new Bullet(color (255, 0, 0), intX, height-30) );
      A = 15;
      println("pew pew");
    }
  }
}

void display2() {
  textAlign(CENTER);
  fill(255, 0, 0);
  text("GAME OVER", width/2, height/2);
}

void pause() {
  oneClick = oneClick-1;
  rectMode(CORNER);
  fill(250, 250, 0);
  rect(250, 10, 40, 20);
  fill(0);
  text("pause", 250, 20);
  fill(255);
  if (mouseX>250 && mouseX<290 && mouseY>10 && mouseY<50 && mousePressed == true) {
    if ( oneClick<0) {
      if ( pause == false) {
        pause = true;
        oneClick = 20;
      } else {
        pause = false;
        oneClick = 20;
      }
    }
  } else {
    if (keyPressed) {
      if (key == 'p' || key == 'P') {
        if ( oneClick<0) {
          if ( pause == false) {
            pause = true;
            oneClick = 20;
          } else {
            pause = false;
            oneClick = 20;
          }
        }
      }
      if (key == 'u' || key == 'U') {
        upScreen = true;
        pause = false;
      }
    }
  }
  if (pause == true) {
    text("paused", width/2, height/2);
    fill(250, 250, 0);
    rect(100, 300, 40, 20);
    fill(0);
    textAlign(CENTER);
    text("restart", 120, 310);    
    if (mousePressed == true && mouseX <= 140 && mouseX >= 100 && mouseY <= 320 && mouseY >= 300) {
      lives = 5;
      score = 0;
      for (Enemy enemy : enemies) enemy.shot = true;
      for (Bullet bullet : bullets) bullet.xBullet = -150;
      for (Enemy2 enemy2 : enemies2) enemy2.shot = true;
      for (Enemy3 enemy3 : enemies3) enemy3.shot = true;
      intX = 150;
      pause = false;
    }
    fill(250, 250, 0);
    rect(150, 300, 70, 20);
    fill(0);
    text("upgrades", 180, 310);
    if (mousePressed == true && mouseX <= 220 && mouseX >= 150 && mouseY <= 320 && mouseY >= 300) {
      upScreen = true;
      pause = false;
    }
    textAlign(LEFT);
    fill(255);
  }
}

void upgrades() {
  background(0);
  fill(250, 250, 0);
  rect(250, 10, 40, 20);
  fill(0);
  text("back", 250, 20);
  fill(255);
  text(money, 40, 20);
  if (mouseX>250 && mouseX<290 && mouseY>10 && mouseY<50 && mousePressed == true) {
    upScreen = false;
  }
}

class Bullet {
  color c;
  float xBullet;
  int yBullet;
  int SIZE = 5;
  boolean shot = false;

  Bullet(color tempC, float tempX, int tempY) {
    c = tempC;
    xBullet = tempX;
    yBullet = tempY;
  }

  void draw() {
    if (shot == false) {
      move();
      display();
      hit();
    }
  }

  void move() {
    yBullet = yBullet - 5;
  }

  void display() {
    rectMode(CENTER);
    noStroke();
    fill(c);
    rect(xBullet, yBullet, SIZE, SIZE);
  }  

  void hit() {
    for (Enemy enemy : enemies) {
      if (xBullet >= enemy.xEnemy-15 && xBullet <= enemy.xEnemy+15 && yBullet >= enemy.yEnemy-15 && yBullet <= enemy.yEnemy+15) {
        enemy.shot = true;
        shot = true;
        score = score + 1;
        money = money + 0.5;
        println("BOEM");
      }
    }
    for (Enemy2 enemy2 : enemies2) {
      if (xBullet >= enemy2.xEnemy-15 && xBullet <= enemy2.xEnemy+15 && yBullet >= enemy2.yEnemy-15 && yBullet <= enemy2.yEnemy+15) {
        enemy2.shot = true;
        shot = true;
        score = score + 3;
        money = money + 1.5;
        println("BOEM");
      }
    }
      for (Enemy3 enemy3 : enemies3) {
        if (xBullet >= enemy3.xEnemy-15 && xBullet <= enemy3.xEnemy+15 && yBullet >= enemy3.yEnemy-15 && yBullet <= enemy3.yEnemy+15) {
          enemy3.shot = true;
          shot = true;
          score = score + 5;
          money = money + 2.5;
          lives = lives + 1;
          println("BOEM");
      }
    }
  }
}

class Enemy2 extends EnemyOverall {
  float xSpeed = 0.75;

  Enemy2(float tempY, color tempC) {
    yEnemy = tempY;
    c = tempC;
  }

  void draw() {
    if (shot == false) {
      move();
      display();
    } else {
      xEnemy = -50;
    }
  }

  void move() {
    xEnemy = xEnemy + xSpeed;
    if (xEnemy >= width) {
      shot = true;
    }
  }
  void display() {
    rectMode(CENTER);
    noStroke();
    fill(c);
    rect(xEnemy, yEnemy, SIZE, SIZE);
  }
}

class Enemy3 extends EnemyOverall {
  float xSpeed = 0.5;
  boolean speedup = true;

  Enemy3(float tempY, color tempC) {
    yEnemy = tempY;
    c = tempC;
  }

  void draw() {
    if (shot == false) {
      move();
      display();
    } else {
      xEnemy = -50;
    }
  }

  void move() {

    if(xEnemy <= width/2 && speedup == true){
      xSpeed = xSpeed + 0.025;
    } else {
      xSpeed = xSpeed - 0.025;
      speedup = false;
    }
    if ( xEnemy < 0) {
      shot = true;
    }
    xEnemy = xEnemy + xSpeed;
  }
  void display() {
    rectMode(CENTER);
    noStroke();
    fill(c);
    rect(xEnemy, yEnemy, SIZE, SIZE);
  }
}

class Enemy extends EnemyOverall {

  Enemy(float tempX, color tempC) {
    xEnemy = tempX;
    c = tempC;
  }

  void draw() {
    if (shot == false) {
      move();
      display();
    } else {
      xEnemy = -50;
    }
  }

  void move() {
    yEnemy = yEnemy + 1;
    if (yEnemy >= height) {
      shot = true;
      lives = lives-1;
    }
  }
}

class EnemyOverall{
  color c = color(255);
  float xEnemy;
  float yEnemy = 0;
  int SIZE = 20;
  boolean shot = false; 

    void display() {
    rectMode(CENTER);
    noStroke();
    fill(c);
    rect(xEnemy, yEnemy, SIZE, SIZE);
  }
}

class Upgrade{
  float cost;
  int X;
  int Y;
  color C = color(255, 0, 0);
  color C2 = color(0, 255, 0);
  boolean click=false;
  int bought = 0;
  int maxBuy;

  Upgrade(int tempX, int tempY, float tempCost, int tempMaxBuy){
    X = tempX;
    Y = tempY;
    cost = tempCost;
    maxBuy = tempMaxBuy;
  }

  void draw(){
    if ( bought<= maxBuy){
    display();
    buy();
    }
  }
  void display(){
    pushMatrix();
    translate(X, Y);
    rectMode(CENTER);
    noStroke();
    fill(C);
    if(money>=cost){
      fill(C2);
    }
    rect(0, 0, 100, 50);
    textSize(30);
    fill(255);
    textAlign(RIGHT);
    text(String.format("%.0f", cost), 50, 0);
    textSize(20);
    text("speed", 10, -10);
    textSize(12);
    rectMode(CORNER);
    textAlign(LEFT);
    popMatrix();

  }

  void buy(){
    if (money>=cost){
      if (mouseX>= X-50){
        if (mouseX <= X+50){
          if (mouseY >= Y-25){
            if (mouseY <= Y+25){
              if(mousePressed == true){
                if (click == false){  
                click = true;
                money = money-cost;
                cost = cost*(1.1);
                shipSpeed=shipSpeed+0.5;
                bought = bought+1;
                } 
              }
            }
            if(mousePressed == false){
              click = false;
            }
          }
        }
      }
    }
  }
}

oh yeah, sorry about the if's inside if's, i made that sometime ago

Answers

  • Here part of an old code of mine:

    Alien[] alien;
    
    void setup(){
      alien = new Alien[20];
    }
    
    void draw(){
      for (int h=0; h<20; h++){
        alien[h].move();
      }
    }
    
    class Alien {
      float x, y, x1,y1;
      int d;
      long t;
    
      Alien(float x, float y, float x1, float y1, int d, long t){
        this.x=x;
        this.y=y;
        this.x1=x1;
        this.y1=y1;
        this.d=d;
      }
    
      .
      .
      .
      .
    }
    

    What you actually do is store the objects in an array and when you reach the end of the array you start refilling it from place 0. You 'll have to alter your code a bit if you want to try this but it worked fine for me.

  • You're already using .add to add objects to an arraylist; .remove should remove it for you.

Sign In or Register to comment.