Unable to increment the value!!

edited September 2016 in Questions about Code

Nmaste all, a very basic question, why won't processing increase the value of mouseX in the given code? if i use "mouseX++" instead of "x++" it does increase it

void setup() {
  size(600, 600);
  background(0);
  stroke(255);
  fill(255, 10);
}

float a = 0;

void draw() {
  float x = mouseX;
  float y = mouseY;

  point(x, y);
  x++;
}

Thanks :)

Tagged:

Answers

  • edited September 2016 Answer ✓

    Both x & y variables were declared inside function draw(): float x = mouseX, y = mouseY;
    Therefore once draw() ends, both x & y cease to exist as well, for their lifespan is scoped to draw(). :-B

  • But draw will end only if I press the stop button, till then it should keep updating the x,y values which it does, only thing is I think its not running the X++ line? dunno..... I m a bit confused here.

  • Answer ✓

    no, draw() is called again and again, automatically. but each call is separate. and variables local to draw() are only available during that one call.

    don't change mouseX and mouseY, they are built in and should only be read, not modified.

  • edited September 2016 Answer ✓

    Function draw() is automatically called back by Processing at about 60 FPS by default!
    Therefore, both x & y variables are "born" and "die" at about 60 FPS! @-)

    https://Processing.org/reference/draw_.html

  • Thanks a lot guys I got it now :D

Sign In or Register to comment.