How to append images to array.

edited April 2018 in Questions about Code

Hi the current code about car and hammer, when running my code will display one car and when break the car will display car broke but my collision code doesn't produce new car automatically when breaking the car. where is my fault? any help Thanks

PImage car, crashed, hammer, broke;
float[] x, y;  // car locations array
float[] shot;  // car broke binary boolean array, 1 = broke, 0 = not broke
int score=0;  // increments when broke.

void setup() {
  size(800, 400);
  x=new float[0];
  y=new float[0];
  shot=new float[0];
  car =loadImage("car.png") ;
  crashed =loadImage("crashed.png") ;
  hammer =loadImage("hammer.png") ;
  broke =loadImage("broke.png");

  x =append(x, mouseX); // random location
  y =append(y, mouseY);
  shot =append(shot, 0); // used as a boolean and matches to each individual car, 0 = car not broke, 1 = broke.
}

void traffic() { // draw the cars in memory to the screen.
  for (int i=0; i<x.length; i++) {
    if (shot[i]==1) { 
      image(crashed, width/2, height/2);
    } else { 
      image(car, width/2, height/2);
    }
  }
}

void collision() { // detect collision between hammer and car
  for (int i=0; i<shot.length; i++) { 
    if (x[i]>=shot[i] && x[i]<=(shot[i]+30) && y[i]>=shot[i] && y[i]<=(shot[i]+10)) 
      shot[i] = 1; 
      x =append(x, mouseX); // random location when old cars crashed.
      y =append(y, mouseY);
      shot =append(shot, 0);
      score++; //increment score
    }
  }
}

void draw() { 
  background(255);
  traffic(); 
  fill(0);
  textSize(20);
  text("score: ", score, 50, 50);
  if (mousePressed) { // image swap
    collision();
    image(hammer, mouseX, mouseY);   //draw hammer image to around mouse locaiton 
  } else {
    image(broke, mouseX, mouseY); // if not pressed then alternative image.
  }
}

Answers

  • First suggestion: x=new float[1];

    Second suggestion: Move this next to void mousePressed(){...} instead.

      if (mousePressed) { // image swap
        collision();
        image(hammer, mouseX, mouseY);   //draw hammer image to around mouse locaiton 
      } else {
        image(broke, mouseX, mouseY); // if not pressed then alternative image.
      }
    

    This doesn't solve the issue but it is a start. Why do you want to append to the arrays? It is possible ArrayList could more suitable for you:

    https://processing.org/reference/ArrayList.html

    Kf

  • the first suggestion will produce stop the program

  • edited April 2018

    The append seems ok (it's also legit to use append instead of ArrayList for a start), BUT:

    you append to x and y but you show all images in the same spot?

    here:

    if (shot[i]==1) { 
          image(crashed, width/2, height/2);
        } else { 
          image(car, width/2, height/2);
        }
    

    maybe

        image(crashed, x[i], y[i] );
        } else {
        image(car,  x[i], y[i]  );    
    

    NEXT

    this

       x =append(x, mouseX); // random location when old cars crashed.
       y =append(y, mouseY);
    

    should be RANDOM:

      x =append(x, random(width)); // random location when old cars crashed.
      y =append(y, random(height));
    

    Same in setup() as in collision:

      x =append(x, mouseX); // random location
      y =append(y, mouseY);
    

    should be

      x =append(x, random(width)); // random location in setup() 
      y =append(y, random(height));
    
  • edited April 2018

    I would use this

    image(hammer, mouseX, mouseY);   //draw hammer image to around mouse location
    

    throughout, meaning outside the if clause right after traffic() so the hammer is shown always.

    You can combine this with noCursor(); in steup() to hide the mouse cursor. Thus only the hammer is displayed throughout

  • edited April 2018

    you got this fancy line which is nonsense:

    if (x[i]>=shot[i] && x[i]<=(shot[i]+30) && y[i]>=shot[i] && y[i]<=(shot[i]+10)) 
          shot[i] = 1; 
    

    because shot is just telling you if the car number i is broken or not

    You can't check position against it!!!!!!!!!!!!!!!!!!!!!!

    instead you want to check mouseX and mouseY against all cars x[i] y[i]

    BESIDES

    BESIDES it's vital that the line ends with { to enclose the whole section into what happens when if clause is true.

    without { only shot[i] = 1; is in the if clause, see indents here:

    void collision() { // detect collision between hammer and car
      for (int i=0; i<shot.length; i++) { 
        if (x[i]>=shot[i] && x[i]<=(shot[i]+30) && y[i]>=shot[i] && y[i]<=(shot[i]+10)) 
          shot[i] = 1;  // !!!!!!!!!!!!!!!!!!!!!!! end 
        x =append(x, mouseX); // random location when old cars crashed.
        y =append(y, mouseY);
        shot =append(shot, 0);
        score++; //increment score
      }
    }
    

    with {...}

    void collision() { // detect collision between hammer and car
      for (int i=0; i<shot.length; i++) { 
        if (x[i]>=shot[i] && x[i]<=(shot[i]+30) && y[i]>=shot[i] && y[i]<=(shot[i]+10)) {
          shot[i] = 1; 
          x =append(x, mouseX); // random location when old cars crashed.
          y =append(y, mouseY);
          shot =append(shot, 0);
          score++; //increment score
        }//if
      }//for
    }//func
    

    (I think you had the bracket { once because you have the correspondent closing } )

  • you can get auto-indent to see those vital errors by hitting ctrl-t in processing

    I use it all the time

  • Thanks Chrisir for all, but when running the code will display 1 car but I can't shot it because x,y and shot arrays=0(setup) whereas when change x,y and shot arrays to 1 (setup) will display 2 cars and I can shot one car only , why?

  • post your entire code

  • I can't shot it because x,y and shot arrays=0(setup)

    in setup

      x =append(x, random(width)); // random location in setup() 
      y =append(y, random(height));
    
  • //I mean I can't shot it because x,y and shot arrays=0(setup) 
    
    x=new float[0];
      y=new float[0];
      shot=new float[0];
    
    -------
    //display 2 cars only one shot
     x=new float[1];
      y=new float[1];
      shot=new float[1];
    
  • the current code will display 2 cars but only one shot :

    PImage car, crashed, hammer, broke;
    float[] x, y;  // car locations array
    float[] shot;  // car broke binary boolean array, 1 = broke, 0 = not broke
    int score=0;  // increments when broke.
    
    void setup() {
      size(800, 400);
      x=new float[1];
      y=new float[1];
      shot=new float[1];
      car =loadImage("car.png") ;
      crashed =loadImage("crashed.png") ;
      hammer =loadImage("hammer.png") ;
      broke =loadImage("broke.png");
    
      x =append(x, random(width)); // random location
      y =append(y, random(height));
      shot =append(shot, 0); // used as a boolean and matches to each individual car, 0 = car not broke, 1 = broke.
    }
    
    void traffic() { // draw the cars in memory to the screen.
      for (int i=0; i<x.length; i++) {
        if (shot[i]==1) { 
          image(crashed, x[i], y[i]);
        } else { 
          image(car, x[i], y[i]);
        }
      }
    }
    
    void collision() { // detect collision between hammer and car
      for (int i=0; i<shot.length; i++) { 
        if (x[i]>=shot[i] && x[i]<=(shot[i]+30) && y[i]>=shot[i] && y[i]<=(shot[i]+10)){ 
          shot[i] = 1; 
          x =append(x, random(width)); // random location when old cars crashed.
          y =append(y, random(height));
          shot =append(shot, 0);
          score++; //increment score
        }
      }
    }
    
    void draw() { 
      background(255);
      traffic(); 
      fill(0);
      textSize(20);
      text("score: ", score, 50, 50);
      if (mousePressed) { // image swap
        collision();
        image(hammer, mouseX, mouseY);   //draw hammer image to around mouse locaiton 
      } else {
        image(broke, mouseX, mouseY); // if not pressed then alternative image.
      }
    }
    

    if change x=new float[0], y=new float[0], shot=new float[0] will display 1 car but no shot

  • edited April 2018

    I understand hammer and car and crashed but what's the broke image??

    as said this is so wrong

    if (x[i]>=shot[i] && x[i]<=(shot[i]+30) && y[i]>=shot[i] && y[i]<=(shot[i]+10)){ 
    
  • You want something like

    if(dist(mouseX,mouseY,x[i],y[i]) < 50) {
    

    (see dist() in the reference)

    OR

    if ( mouseX > x[i]-10 &&
         mouseX < x[i]+30 &&
         mouseY > y[i]-10 &&
         mouseY < y[i]+30 && ) {
    
  • edited April 2018

    Thanks for all, the broke image is an alternative image for hammer , and about your collision code if(dist(mouseX,mouseY,x[i],y[i]) < 50) and the second code will display 1 car and when shot it will display crashed car image with more than 10 new cars image, each shot will produce again 10 new cars. Thanks again I will try to fix it.

  • Answer ✓

    as for the ten times issue:

    read again

    Second suggestion: Move this next to void mousePressed(){...} instead.

    above, by kfrajer.

    do this, and it will be healed

  • Thanks, Chrisir and kfrajer

  • edited April 2018

    please post your entire code so we can see the result

    thank you

Sign In or Register to comment.