Trouble making the animation pause via a key press

I'm trying so when i press any key that the ellipses stop in animation and resume when the key is pressed but i'm not too sure on how to do it.

I'm new to processing and I don't want to use more advanced stuff like noloop, etc. Could anyone give me any ideas on what to do?

Code follows:

float x=-2500;
float y=-2500;
int x2=70;
int y2=70;
int a=45; //for setting the circles behind the display 
int ellipseG=250; //variable for ellipse (green)
int ellipseB=350; //variable for ellipse (blue)
float speedX=2.7;
float speedY=2;


void setup() {
  size(700,500);
  background(255);
  smooth(); //Makes all geometry smoother
}


void draw () {

  fill(255,0,0); //makes vertical rectangle red
  noStroke(); // removes outline
  rect(315,-10,70,550); //coordinates for verticle rectangle
  rect(-10,215,750,70); //coordinates for horizontal rectangle


  // circles
  fill(000,255,000); //makes horizontal circle green
  ellipse(x-a,ellipseG,x2,y2);
  x=x+speedX; //adjusts speed for green circle


  fill (000,000,255); //makes vertical circle blue
  ellipse(ellipseB,y-a,x2,y2);
  y=y+speedY; //adjusts speed for blue circle
}

void mousePressed () {
  x= 0;
  y =0;  //Returns all x and y values to the 0 axis, essentially restarting the animation
}

void keyPressed() {


}

Answers

  • edited December 2017

    ... and I don't want to use more advanced stuff like noLoop(), etc.

    IMO, the noLoop() + loop() "trick" isn't advanced. :-\"

    Regardless, you still can declare some boolean variable in order to act as a flag, whether the sketch is "paused" or not. ;)

    Let's just call it paused. :)

    Then switch it "on" & "off" inside keyPressed(). :>

    And then return prematurely from draw() when paused is true. *-:)

    boolean paused;
    
    void draw() {
      if (paused)  return;
    
      // rest of draw() below:
    }
    
    void keyPressed() {
      paused ^= true; // same as: paused = !paused;
    }
    
Sign In or Register to comment.