Moving object when mousePressed

Can anyone help me out with this problem?

When mouse is pressed in the top half of the window then, a circle appears. Pressing the mouse in the bottom half of the window has no effect. Any previous shape disappears when the new shape appears The shape moves down the window, two pixels at a time, until it completely disappears off the bottom of the window

I tried this code but, it doesnt work when the circle crosses the other half. :/

int x = -1000, y;
int diam = 50;

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

void mousePressed(){
  if (y <= height/2) {
    x = mouseX;
    y = mouseY;
  }
  //else {
  //}
}
void draw() {
  background(0);
  ellipse(x, y, diam, diam);
  y++;
}
Tagged:

Answers

  • When mouse is pressed in the top half of the window then, a circle appears. Pressing the mouse in the bottom half of the window has no effect. Any previous shape disappears when the new shape appears The shape moves down the window, two pixels at a time, until it completely disappears off the bottom of the window

    Is this a homework problem?

    Which part that you are not able to get working -- where are you stuck?

  • Ah. You have a very small typo/bug.

      if (y <= height/2) {
        x = mouseX;
        y = mouseY;
      }
    

    This says "If the y value of the ball is in the upper half of the screen, update x and y to the mouse location."

    That is wrong. What should it say instead?

  • edited August 2017 Answer ✓

    This is the code.

        // Task A
    
        // Global variables
        int x = -1000, y, click, speed = 2;
        int diam = 50;
    
        // Window output
        void setup() {
          size(400, 400);
        }
    
        void mousePressed() {  
          if (mouseY < height/2) { 
            // mousePressed won't work if the coordinates are at the bottom half
            x = mouseX;
            y = mouseY;
          }
          // Task D
          // the shape will always go down when mousePressed
          if (speed < 0) {
            speed *= -1;
          }
          /** assigning random numbers between 1-10 to the variable 'click' so that there is an
          equal chance of square or circle to appear. */
          click = (int)(random(0, 11));;
        }
    
        void draw() {
          background(255);// white background
    
          if(click >= 5) { //if click is greater then or equal to 5 square will appear
            // Task B
            /** When the square reaches the bottom half of the window, its colour becomes red
            on the left half of the window and green if it is on the right. */
            if (x <= width/2 && y >= height/2) {
              fill (255, 0, 0); // red
            }
            else if (x >= width/2 && y >= height/2) {
              fill(0, 255, 0); // green
            }
            else {
              noFill();
            }
            rectMode(CENTER); // center of mousePressed
            rect(x, y, diam, diam);
          }
    
          else if (click < 5) {
            // Task B
            /** When the circle reaches the bottom half of the window, its colour becomes red,
            on the right half of the window and green if it is on the left*/
            if (x >= width/2 && y >= height/2) {
              fill (255, 0, 0); // red
            }
            else if (x <= width/2 && y >= height/2) {
              fill(0, 255, 0); // green
            }
            else {
              noFill();
            }
            ellipseMode(CENTER); // center of mousePressed
            ellipse(x, y, diam, diam);
          }
    
          // the shape goes down by 2 pixels
          y += speed;
          // Task D
          // the shape rebouds up and down the window
          if (y > height || y < 0) {
            speed *= -1;
          }
    
        }
    
        // Task C
        /** Pressing the ‘q’ key moves the shape two pixels to the left 
        (but doesn’t otherwise affect the downward motion). Similarly, 
        pressing ‘p’ moves the shape to the right. */
        void keyPressed() {
         if (key == 'q') {
           x -= 2; // moves 2 pixels to the left  
         }
         if (key == 'p') {
           x +=2;  //moves 2 pixels to the right
         }
        })
    
  • Answer ✓

    Does it work? Solved?

Sign In or Register to comment.