Circle grows on left side of screen, not right

Hello, I’m new to programming and I was just playing around with processing.

I created a program where a circle follows your mouse and grows if it touches the edges of the screen. For some reason it works only when the circle is on the left side of the screen, but not the right.

Can someone help me out. Here is the code:

int size = 20;

void setup(){
  size(800, 800, P2D);
}

void draw(){
  background(0);
  fill(200, 120, 60);
  ellipse(mouseX, mouseY, size, size);
  if(mouseX == width || mouseX == 0){
    size++;
  }
}

Thanks!

1 Like

Have you tried debugging your code? Here’s a guide that might help you:

For example, if I were you I would print out the value of the mouseX variable. I think you’ll find that it doesn’t quite get to the exact value of the width variable.

This is because if you have a window that’s 800 pixels wide, then the rightmost pixel is 799. This is because the leftmost pixel is 0 instead of 1.

Also, I think you’ll find that if you move the mouse very fast, the value of mouseX can move by several pixels each frame and the value will not be exactly 0 or width - 1 anyway. You might want to check for a distance instead of an exact value.

Here’s another guide that might come in handy:

2 Likes

I understand now, I got it working.

Thanks!

Screen width is 800. Coordinates starts from 0 on the left side, so coordinate of the right side is 799. This applies to arrays or any other indexing starting from zero. You need to decrease one from the end to get the last index or a coordinate in this case.

Try this instead:

if(mouseX == width-1 || mouseX == 0){

Note that this does not solve the other problem I pointed out.

True. I should have read your comment properly.

It’s going to be running on full screen so mouse speed doesn’t really matter.

Thanks for the help guys