Change fill of multiple objects at the same time


#1

Hi guys, im using a variable to loop a robot and i need a especific robot to change its entire color.
Do you guys now how to change the fill of multiple objects?


#2
  for (int i=0; i < 5; i++) {
    for (int k = 0; k < 2; k++){
      centrox = i*350+200;
      centroy = k*400+300;
      if (i == 3 && k == 1) {
      // every object that has fill(210,175,61); need to change to another color
  
      }

#3

You could create a color variable for every object and test if that variable equals a certain value (more on that datatype here: https://processing.org/reference/color_datatype.html).
You can check if two values are equal by using the “==” operator and the function color() around the value (210, 175, 61). Example:

color color1 = color(210, 175, 61);

void setup(){
  if(color1 == color(210, 175, 61)){
    //change color
  }else{
    //something
  }
}

If you need to know how to test this for every object, I’d need to know how you store the robots. (Are they instances of a class? Do you store them in an Array/ArrayList or separatly?) Also posting more of your code could help. You can mark it as such by putting three ` at the start and the end. :smile:


#4

Exactly. If these are objects of a Robot class, then you can assign their color as a class property when you create them, and use that property when you draw them:

ArrayList<Robot> robots;

void setup() {
  robots = new ArrayList<Robot>();
  color c;
  for (int i=0; i<100; i++) {
    c = color(random(255), random(255), random(255));
    robots.add(new Robot(random(width), random(height), c));
  }
}

void draw(){
  background(128);
  for(Robot r : robots){
    r.display();
  }
}

class Robot {
  PVector loc;
  color c = color(255);
  Robot( float x, float y, color c ) {
    this.c = c;
    this.loc = new PVector(x, y);
  }
  void display() {
    pushStyle();
    fill(c);
    rect(loc.x, loc.y, 10, 10);
    popStyle();
  }
}

See for example the objects tutorial:

https://processing.org/tutorials/objects/