How do I change cursor relative to a randomly positioned shape

I want to have a rectangle appear in a random location within a canvas each time the mouse is clicked.

But with each new location I want the cursor to change as you move it from left to right (and back) of the rectangle. however i cant figure out how to do this?

Below is what I have so far:

void setup() {
  size(1000, 600);
  background(15, 85, 115);
  noLoop();
}

void draw() {

  float boxw = 100;
  float boxh = 150;
  float boxx = random(0, width-boxw);
  float boxy = random(0, height-boxh);


  background(15, 85, 115);
  stroke(0);
  fill(#75EDEC);
  rect(boxx, boxy, boxw, boxh);

  if (mouseX>(boxx+(boxw/2))) {
    cursor(CROSS);
  } else{
    cursor(HAND);
  }
}

void mouseClicked() {
  redraw();
}

thanks for your help! :D

Tagged:

Answers

  • edited June 2016

    So you are trying to draw a randomly sized rectangle placed anywhere on the canvas. Could you elaborate on what you mean with the cursor moving? Are you trying to move the cursor using processing?

  • you have to check more with if

    if(mouseX>boxx && mouseX< boxx+boxw&&

    mouseY.......

  • edited June 2016

    Chrisir, for now I'm only worried about the cursor changing as it passes the midpoint of the rectangle

    Rbhog, the order of play should be like this:

    • user clicks mouse

    • processing draws rectangle in random location

    • user moves mouse

    • processing changes cursor shape depending on whether it is to the left or right of the new rectangle

    • Repeat

  • Answer ✓

    Why noLoop and redraw?

    Move lines 11&12 into mouseClicked (and make them global vars)

  • chrisir, if i make 11&12 global vars then the random selection happens only once, I want the above process to happen each time the mouse is clicked (sorry should have mentioned that)

  • Answer ✓

    I said move the random part into mouseClickef

  • oooh! haven't tried that yet, will get back to you!

  • Answer ✓

    Get rid of noloop

  • It works! Thanks Chrisir =)

    working code below:

    void setup() {
      size(1000, 600);
      background(15, 85, 115);
    
    }
    
    float boxw = 100;
      float boxh = 150;
      float boxx;
      float boxy;
    
    void mouseClicked() {
      boxx = random(0, width-boxw);
      boxy = random(0, height-boxh);
    }
    
    
    void draw() {
    
      float boxw = 100;
      float boxh = 150;
    
    
      background(15, 85, 115);
      stroke(0);
      fill(#75EDEC);
      rect(boxx, boxy, boxw, boxh);
    
      if (mouseX>(boxx+(boxw/2))) {
        cursor(CROSS);
      } else{
        cursor(HAND);
      }
    }
    
  • well done!

Sign In or Register to comment.