How can I delete a spot in an array

I'm sure this question has been asked and answered billions of times but I can't find anything that works for me. i am making a Break Out type game and I have the bricks setup in arrays, and when the ball hits a brick I want the brick to stop being drawn so I want to take it out of the array. If there is any other way to solve this problem that would be helpful too, thanks!

Oh and here is my code (at least that part of it):

    for (var i = 0; i < 8; i++) {
            brickX = i * 50;
            bricks1[i] = brickX + (i + 1) * 11; 
            bricks2[i] = brickX + (i + 1) * 11;
            bricks3[i] = brickX + (i + 1) * 11;
            bricks4[i] = brickX + (i + 1) * 11;
            fill(0, 0, 255);
            rect(bricks1[i], 10, 50, 10);
            fill(255, 0, 0);
            rect(bricks2[i], 30, 50, 10);
            fill(0, 255, 0);
            rect(bricks3[i], 50, 50, 10);
            fill(100, 0, 100);
            rect(bricks4[i], 70, 50, 10);
            if (ball.x >= bricks1[i] && ball.x <= bricks1[i] + 50 && ball.y >= 10 && ball.y <= 10 + 10) {
                bricks1[i] = 800;
                ball.yDir *= -1;
            } else if (ball.x >= bricks2[i] && ball.x <= bricks2[i] + 50 && ball.y >= 10 && ball.y <= 30 + 10) {
                bricks2[i] = 800;
                ball.yDir *= -1;
            } else if (ball.x >= bricks3[i] && ball.x <= bricks3[i] + 50 && ball.y >= 10 && ball.y <= 50 + 10) {
                bricks3[i] = 800;
                ball.yDir *= -1;
            } else if (ball.x >= bricks4[i] && ball.x <= bricks4[i] + 50 && ball.y >= 10 && ball.y <= 70 + 10) {
                bricks4[i] = 800;
                ball.yDir *= -1;
            }
        }
Tagged:

Answers

  • Edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format it. Make sure there is an empty line above and below your code.

    Kf

  • I thought this would work but then I noticed my real problem which was even after deleting spots they would get put back the next time the for loop runs

  • edited July 2017 Answer ✓

    Arkanoid!!! I love it.

    You could define your bricks in setup and display them in draw().

    You need to change your data structure I am afraid - you need to store each brick separately and not column wise.

    You could make parallel arrays for x position for y position and for color and for isAlive.

    So you have arrays brickX, brickY, brickColor, brickIsAlive

    Each the same size.

    Element 0 in each array gives you the distributed data for one brick (number 0).

    To delete one brick just set brickIsAlive[i] to false and don't display it (and don't check it for collisions).

    Discussion:

    https://forum.processing.org/two/discussion/8082/from-several-variables-to-arrays

    object oriented programming

    Later check the technical faq for getting from here to object oriented programming:

    https://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes

  • You're welcome!

  • Here is a version with object oriented programming.

    Click on the bricks to delete them.

    Best, Chrisir ;-)

    ArrayList<Brick> bricks = new ArrayList(); 
    
    void setup() {
    
      size(1000, 800);
      background(0);
    
      initBricks();
    }//func 
    
    void draw() {
    
      background(0);
    
      for (Brick currentBrick : bricks) { 
        currentBrick.display();
      } // for
    }//func 
    
    // -------------------------------------------------
    // help functions 
    
    void mousePressed() {
      for (Brick currentBrick : bricks) { 
        if (currentBrick.exists)
          if (currentBrick.mouseOver())
            currentBrick.kill();
      } // for
    }//func
    
    void initBricks() {
    
      // size of one brick
      float w = 60; 
      float h = 20; 
    
      // how many bricks do we have Horizontally 
      int numberHorizontally = 10; 
      int numberVertically = 3; 
    
      // how much space is not covered by the bricks Horizontally (in pixels)
      float spaceHorizontally = width - (w*numberHorizontally); 
      // half of which we add on the left so the bricks are centered
      float offsetX= spaceHorizontally/2;
    
      // ...and what we add at the top to have a space from the top of the screen
      float offsetY= 100;
    
      for (int x = 0; x<numberHorizontally; x++) { 
        for (int y= 0; y<numberVertically; y++) {
          // Using "if" to give it a small variation
          if (x!=y) {
            Brick newBrick=new Brick(x*w+offsetX, y*h+offsetY, w, h);
            bricks.add(newBrick);
          }//if
        }
      }
    }//func 
    
    // =================================================
    
    class Brick {
    
      float x, y; 
      float w, h;
    
      color col=color(random(100, 255), random(100, 255), random(0, 255));
    
      boolean exists=true; 
    
      // constr 
      Brick(float x_, float y_, 
        float w_, float h_) {
    
        x=x_;
        y=y_;
        w=w_;
        h=h_;
      }//constr
    
      void display() {
        if (!exists)
          return; 
    
        fill(col);
        rect(x, y, w, h);
      }//method 
    
      boolean mouseOver() {
        return 
          mouseX>x&&mouseX<x+w&&
          mouseY>y&&mouseY<y+h;
      }//method
    
      void kill() {
        exists=false;
      }//method
      //
    }//class
    //
    
Sign In or Register to comment.