How do I make my ellipse move only horizontally if I click my mouse and if I click it again it ...

How do I make my ellipse move only horizontally if I click my mouse and if I click it again it moves as it did before? This is my code:

int xPosition, yPosition;
int xAdder = 3;
int yAdder = 7;
int r, g, b;
boolean rIncreasing = true;

void setup() {

  size(displayWidth/2, displayHeight); 
  xPosition = 200;
}

void draw() {

  background(255, 0, 255);

  fill(r, g, b);

  ellipse(xPosition, yPosition, 100, 100);

  xPosition = xPosition + xAdder;
  yPosition = yPosition + yAdder;

  if (xPosition > displayWidth/2) {
    xAdder = -3;
  }
  if (yPosition > displayHeight) {
   yAdder = -7; 
  }

  if (xPosition < 0) {

    xAdder = 3;
  }

  if (yPosition < 0) {

    yAdder = 7;
  }

  if (rIncreasing) {
    r = r + 1;
  } 
  else {
    r = r - 1;
  }

  if (r > 255) {
    rIncreasing = false;
  } 
  else if (r < 0) {
    rIncreasing = true;
  }
}

Answers

  • Do you want the ball to move only if you click the mouse? If so, wrap your "movement code" inside a if(){} block that uses mousePressed as the boolean operator in the conditional statement.

    ex.

    int xPosition, yPosition; int xAdder = 3; int yAdder = 7; int r, g, b; boolean rIncreasing = true;
    
    void setup() {
    
      size(displayWidth/2, displayHeight); 
      xPosition = 200; 
    }
    
    void draw() {
    
      background(255, 0, 255);
    
      fill(r, g, b);
    
      ellipse(xPosition, yPosition, 100, 100);
    
      if(mousePressed){
        xPosition = xPosition + xAdder; 
        yPosition = yPosition + yAdder;
    
        if (xPosition > displayWidth/2) { 
          xAdder = -3; 
        } 
        if (yPosition > displayHeight) { 
          yAdder = -7; 
        }
    
        if (xPosition < 0) {    
          xAdder = 3;
        }
    
        if (yPosition < 0) {
          yAdder = 7;
        }
    
        if (rIncreasing) { r = r + 1; } else { r = r - 1; }
    
        if (r > 255) { 
          rIncreasing = false; 
        } else if (r < 0) { 
          rIncreasing = true; 
        } 
      }
    }
    
  • int xPosition, yPosition; 
    int xAdder = 3; 
    int yAdder = 7; 
    int r, g, b; 
    boolean rIncreasing = true;
    boolean toggle = true;
    
    void setup() {
    
      size(400, 600); 
      xPosition = 200;
    }
    
    void draw() {
    
      background(255, 0, 255);
    
      fill(r, g, b);
    
      ellipse(xPosition, yPosition, 100, 100);
    
      xPosition = xPosition + xAdder; 
      if( toggle ){
        yPosition = yPosition + yAdder;
      }
    
      if (xPosition > width) { 
        xAdder = -3;
      } 
      if (yPosition > height) { 
        yAdder = -7;
      }
    
      if (xPosition < 0) {
        xAdder = 3;
      }
    
      if (yPosition < 0) {
        yAdder = 7;
      }
    
      if (rIncreasing) { 
        r = r + 1;
      } 
      else { 
        r = r - 1;
      }
    
      if (r > 255) { 
        rIncreasing = false;
      } 
      else if (r < 0) { 
        rIncreasing = true;
      }
    }
    
    void mousePressed(){
      toggle=!toggle;
    }
    
  • edited October 2013

    A simpler 1 just for comparison: :bz

    // Bumping Ellipse (v2.0)
    // by  Tonakisvn (2013/Oct)
    // mod GoToLoop
    
    // forum.processing.org/two/discussion/374/
    // how-do-i-make-my-ellipse-move-only-horizontally-
    // if-i-click-my-mouse-and-if-i-click-it-again-it-
    
    final static int W = 100, H = 75;
    
    int x, y, spx = 5, spy = 7;
    boolean isBumping = true;
    
    void setup() {
      size(400, 600);
    
      frameRate(60);
      smooth();
    
      ellipseMode(CENTER);
      strokeWeight(4);
    
      stroke(0);
      fill(0200);
    
      x = width  >> 1;
      y = height >> 1;
    }
    
    void draw() {
      background(-1);
      ellipse(x, y, W, H);
    
      if ((x += spx) < W/2 | x > width - W/2)
        spx *= -1;
    
      if ((y += isBumping? spy : 0) < H/2 | y > height - H/2)
        spy *= -1;
    }
    
    void mousePressed() {
      isBumping = !isBumping;
      fill((color) random(#000000));
    }
    
Sign In or Register to comment.