Index size Problem

edited April 2018 in Questions about Code

Hi, this is a problem that I have been having for a while. My program works fine until you fire too many bullets and the array gets bogged down, the thing is, I don't know why it keeps breaking at its limit.

//April 28, 2018

float randomside = 0.0;

ArrayList<Integer> bulletup = new ArrayList<Integer>();
ArrayList<Integer> dotposition = new ArrayList<Integer>();

ArrayList<Integer> balloonX = new ArrayList<Integer>();
ArrayList<Integer> balloonY = new ArrayList<Integer>();

int moveup = 60;
float fastdot = mouseX;
float dotcoordiant = -50;
int lastTimeCheck;
int timeIntervalFlag = 6500;
int timeIntervalFlag2 = 30;

int drawCount = 0;

void setup() {
  // set size of display

  size (500, 720);
  background (256, 256, 256);
  strokeWeight(10);
  frameRate(60);
  lastTimeCheck = millis();
}


void mousePressed(){
  println("the mouse was pressed at ",mouseX);
  dotposition.add(mouseX);
  bulletup.add(650);
}

int distance2(int X1, int Y1, int X2, int Y2){
  int a = X2 - X1;
  int b = Y2 - Y1;
  return(a * a + b * b);
}

boolean isFirstOffTop(ArrayList<Integer> myBullets){
  if (myBullets.size() == 0){
    return(false);
  }
  if (myBullets.get(0) > 0){
    return(false);
  }
  return(true);
}

boolean isFirstOffBottom(ArrayList<Integer> myBullets){
  if (myBullets.size() == 0){
    return(false);
  }
  if (myBullets.get(0) < 780){
    return(false);
  }
  return(true);
}

void draw() {
  background (256, 256, 256);
  randomside = random(450);
  stroke(0, 0, 256);
  point(mouseX, 650);

  //bulletcode

  // remove front bullets that have reached the top
  while (isFirstOffTop(bulletup)){
    bulletup.remove(0);
    dotposition.remove(0);
  }

  // draw the moving bullets
  for (int bi = 0; bi < dotposition.size(); bi++) {
      point(dotposition.get(bi), bulletup.get(bi));
      bulletup.set(bi, bulletup.get(bi) - 6);
  }


  if ( millis() > lastTimeCheck + timeIntervalFlag ) {
    lastTimeCheck = millis();
    moveup = 60;
    println(lastTimeCheck);
  }
  /*if ( millis() > lastTimeCheck + timeIntervalFlag2 ) {
   lastTimeCheck = millis();
   bulletup = bulletup;
   println(lastTimeCheck);

   }
   */

   // make another arraylist of "ballons",
   // on each draw create one new balloon at the top of the screen
   // at random X positions
   // slide every balloon down 10 pixels/frame
  drawCount += 1;
  if (drawCount > 1){
    // generate one more balloon at top of screen
    balloonX.add(int(random(500)));
    balloonY.add(0);
    drawCount = 0;
  }

  // remove balloons that have reached the bottom
  while (isFirstOffBottom(balloonY)){
    balloonX.remove(0);
    balloonY.remove(0);
  }

  // draw the falling balloons
  stroke(256, 0, 0);
  for (int bi = 0; bi < balloonX.size(); bi++) {
      point(balloonX.get(bi), balloonY.get(bi));
      balloonY.set(bi, balloonY.get(bi) + 5);

  }
  println("ballon size = ", balloonX.size());
  // pop balloons that have been hit
  int dist = 10;
  int dist2 = dist * dist;
  // loop backwards so I can delete through indexed loop
  for (int bi = balloonX.size() - 1; bi >= 0; bi--) {
      for (int si = 0; si < dotposition.size(); si++){
        println("ballon size = ", balloonX.size());
        if (distance2(dotposition.get(si), bulletup.get(si),
            balloonX.get(bi), balloonY.get(bi)) <= dist2) {
               balloonX.remove(bi); 
               balloonY.remove(bi); 
            }
      }
  }
  // Kill balloons that have reached the bottom
}

Answers

Sign In or Register to comment.