Alternative color changing problem

int del=200;

void setup() {
    size(700,500);
    rect(200,100,300,300);
    rect(150,220,50,150);
    rect(500,220,50,150);
}

void draw() {
    fill(125,0,0);
    leftred();
    delay(del);
    rightred();
    delay(del);
    leftwhite();
    delay(del);
    rightwhite();
    delay(del);
}

void rightred() {
    colorMode(HSB,500);
    println("2   ");
    fill(10);
    rect(500,220,50,150);
}

void rightwhite() {
    colorMode(HSB,500);
    println("4  ");
    fill(499);
    rect(500,220,50,150);
}

void leftred() {
    colorMode(HSB,500);
    println("1  ");
    fill(10);
    rect(150,220,50,150);
}

void leftwhite() {
    colorMode(HSB,500);
    println("3  ");
    fill(499);
    rect(150,220,50,150);
}

//The above program has two rectangles on each side of the center rectangle whose color must change alternatively from black to white and vice versa. But it doesnt. Can someone explain to me why? Thanks in advance.

Answers

  • Thanks GoToLoop. Is there any other way that I can do it?

  • int del=200;
    int counter=0;
    
    int startTimer;
    
    void setup() { 
      size(700, 500); 
    
      startTimer=millis();
      background(111);
    
    
      background(111); 
    
      rect(200, 100, 300, 300); 
      rect(150, 220, 50, 150); 
      rect(500, 220, 50, 150);
    }
    
    void draw() { 
    
    
      fill(125, 0, 0); 
    
      switch ( counter ) {
      case 0:
        leftred();
        break;  
      case 1:  
        rightred();
        break;  
      case 2:   
        leftwhite();
        break;  
      case 3:  
        rightwhite();
        break;
      }// switch
      if (millis()-startTimer>900) {
        counter++; 
        startTimer=millis();
      }
      if (counter>3)
        counter=0; // reset
    }//func
    
    void rightred() { 
      colorMode(HSB, 500); 
      println("2 "); 
      fill(10); 
      rect(500, 220, 50, 150);
    }
    
    void rightwhite() { 
      colorMode(HSB, 500); 
      println("4 "); 
      fill(499); 
      rect(500, 220, 50, 150);
    }
    
    void leftred() { 
      colorMode(HSB, 500); 
      println("1 "); 
      fill(10); 
      rect(150, 220, 50, 150);
    }
    
    void leftwhite() {
      colorMode(HSB, 500); 
      println("3 ");
    
      fill(499); 
      rect(150, 220, 50, 150);
    }
    
Sign In or Register to comment.