Example 4-3 in the previous chapter moved a circle across the window. Change the sketch so that the circle only starts moving once the mouse has been pressed. Use a boolean variable.

boolean __________ =  _________;  
int circleX = 0;  
int circleY = 100;

void setup()  {   
  size(200,200);  
}   

void draw()  {   
  background(100);  
  stroke(255);  
  fill(0);  
  ellipse(circleX,circleY,50,50);  
  ____________________________________  
  ____________________________________  
  ____________________________________  
} 

void mousePressed()  {   
  ____________________________________  
}
    
Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 5-8: Example 4-3 in the previous chapter moved a circle across the window. Change the sketch so that the circle only starts moving once the mouse has been pressed. Use a boolean variable.

// Boolean variable starts as false
boolean going = false;

// Location of circle
int circleX = 0; 
int circleY = 100; 

void setup() { 
  size(200,200); 
  smooth();
} 
void draw() { 
  background(255); 
  
  // Draw the circle
  stroke(0); 
  fill(175); 
  ellipse(circleX,circleY,50,50); 
  
  // Only move the circle when "going" is true
  if (going) {
    circleX = circleX + 1;
  }
} 

// Set going to true when the mouse is pressed!
void mousePressed() { 
  going = true;
} 
  • Peter

    for sake of exercise I found it interesting to do slightly altered versions of the code in order to

    a) let the circle stop at another click of the mouse
    or
    b) let the circle move on only as long as the mouse is pressed

    it’s rather easy to do once you understand this kind of boolean variables.

  • Jinieheo

    missing excercise 5-7.. :)

  • Diego

    boolean but = false;

    int circleX = 0;

    int circleY = 100;

    void setup() {

    size(200,200);

    }

    void draw() {

    background(100);

    stroke(255);

    fill(0);

    ellipse(circleX,circleY,50,50);

    if (but){

    circleX = circleX + 1;

    }

    // to apear from the outer side

    if (circleX == 225){

    circleX = -25;

    }

    }

    void mousePressed() {

    but = !but;

    }

  • Xny

    Here I created the circle going on as default (boolean is true as default), and with clicking the direction of movement is reversed.

    Is there a way to introduce 3rd click option, which will make the ball stop ?

    1- default; moves forward;
    2- 1st click; changes direction;
    3- 2nd click; stops;

    int circleX = 10;
    int circleY = 100;

    boolean button = true;

    void setup(){
    size(200,200);
    }

    void draw(){
    background(255);
    fill(0);

    ellipse(circleX,circleY,20,20);

    if(button){
    circleX = circleX + 1;
    } else {
    circleX = circleX – 1;
    }

    if(circleX > 190){
    circleX = 190;
    }
    }

    void mousePressed(){
    button = !button;
    }

  • shiffman

    increment a counter variable in mousePressed() to count the number of clicks.

  • Xny

    Answer for 5-7

    If to insert that line code in void draw, it will run the code repeatedly as long as the mouse is pressed. It is visible more clearly if to decrease frameRate (frameRate(4); in void setup) (we will see the sequence of toggling between flase-true slower, while the mose is pressed). Check this;

    boolean button = false;

    int x = 50;

    int y = 50;

    int w = 100;

    int h = 75;

    void setup() {

    size(200,200);

    frameRate(4);

    }

    void draw() {

    if(button) {

    background(255);

    stroke(0);

    } else {

    background(0);

    stroke(255);

    }

    if(mouseX > x && mouseX y && mouseY < y+h && mousePressed){

    button= !button;

    }

    fill(175);

    rect(x,y,w,h);

    }

  • TIN

    This is my code

    boolean mouseClick = false;

    int circleX = 0;

    int circleY = 100;

    void setup() {

    size(200,200);

    }

    void draw() {

    background(100);

    stroke(255);

    fill(0);

    ellipse(circleX,circleY,50,50);

    if (mouseClick==true)

    circleX++;

    }

    void mousePressed() {

    mouseClick=true;

    }