Help with some Crosshairs

please format code with </> button * homework policy * asking questions

So, as part of a class assessment I need to make a spooky halloween scene, so far my code is this

/
if((mouseX >= 100) && (mouseX <= 700) && (mouseY >= 100) && (mouseY <= 500))
{
strokeWeight(5);
line(mouseX,102,mouseX,498);
line(102,mouseY,698,mouseY);
}

My problem is, when I bring my mouse over the ellipse to change it’s colour, the crosshairs disappear, which isn’t too big an issue, the problem is they don’t reappear once I move the mouse back off, how can I fix this?

Hi! Welcome to the forum! :wave:

I guess you have more lines on your code - because I feel the rest of the code is causing the problem. More concretely, you may have noStroke() somewhere to hide lines. But then once noStroke is triggered, even once inside if, the stroke will not come back. You have to call stroke(...) to bring the stroke back, or wrap noStroke() inside push() / pop() to keep the style within the block. Just to illustrate the problem:

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

void draw() {
  background(128);
  if (mousePressed) {
    noStroke();
    ellipse(100, 100, 50, 50);
  }
  strokeWeight(5);
  line(mouseX, mouseY, mouseX+10, mouseY);
}

Once you click, the line will be gone. I think this is the kind of problem you are having. One way to fix is

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

void draw() {
  background(128);
  if (mousePressed) {
    noStroke();
    ellipse(100, 100, 50, 50);
  }
  stroke(0);
  strokeWeight(5);
  line(mouseX, mouseY, mouseX+10, mouseY);
}

and the other (elegant) solution

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

void draw() {
  background(128);
  if (mousePressed) {
    push();
    noStroke();
    ellipse(100, 100, 50, 50);
    pop();
  }
  strokeWeight(5);
  line(mouseX, mouseY, mouseX+10, mouseY);
}

Anyways it’s always a good idea to show the entire code (if the project is big, make sure you make a small enough program to reproduce the problem…)

1 Like