Why does my program return the error, RuntimeException: Too many popStyle() without enough pushStyle()?

why does my program return the error, RuntimeException: Too many popStyle() without enough pushStyle()?
i have the same amount of pop as i do push
(I want to have the screen be inverted, and go back to normal every five minutes)

//visual
PImage shrek;
//timer

//moving
float charx=0;
float chary=0;
boolean invert= false;


void setup() {
  size(1000, 563);
  shrek= loadImage("shrek.jpg");
  chary= height/2;
}

void draw() {
  //visual
  background(shrek);
  color(255);
  ellipse(charx, chary, 50, 50);

  //timer
  println(millis()/1000);
  if (millis()/1000%300==0&&millis()/1000>10&& invert==false) {
    invert=true;
  }
    if (millis()/1000%300==0&&millis()/1000>10&& invert==true) {
    invert=false;
  }
  while (invert==true){
    push();
    filter(INVERT);}
    while(invert==false){
      pop();
    }
  //moving
  if (keyPressed) {
    //move left with a
    if (key == 'a'&&invert==false) {
      charx=charx-10;
    } 

    //move right with d
    if (key == 'd'&&invert==false) {
      charx=charx+10;
    }

    //move right with a
    if (key == 'a'&&invert==true) {
      charx=charx+10;
    } 

    //move left with d
    if (key == 'd'&&invert==true) {
      charx=charx-10;
    }
  }
}

Hello,

The references may help you understand this:

:)

1 Like

i read all three, I still don’t understand

you may have listed only one push and one pop operation but the while loops that contain them are run an uneven amount of times. most likely due to the conditional which controls when to apply them. also you will run into a limit on the amount of push operations you can do (i think it is a max of 32) i would rethink your code or at least explain your goals so others can understand and perhaps offer another point of view on how to achieve it. best of luck.

1 Like