How to get mouseX and mouseY when using noLoop() ?

I've been trying to make a game with different scenes and control scene changes by button clicks. After some debugging I noticed that mouseX and mouseY are returning 0 values when draw() is not called (i.e. during instances where noLoop() is being used).

I found a work around that gives me values close enough to what mouseX and mouseY should be using winMouseX and winMouseY but those seem to be off a few pixels and will change depending on the window frame. Anybody know how to get the actual mouseX and mouseY values?


  • Answer ✓

    Simply don't use noLoop(). Instead, have a boolean that determines if draw() should do anything.

    void draw(){
      if( dontLoop ) return;
      // ...
  • edited January 2016 Answer ✓

    After so many patches to remedy their bugs, p5.js doesn't match noLoop()'s behavior w/ Processing anymore! [-( Actually they don't know nor look up what is supposed to be the proper behavior: ~X(

  • Thank you both for your quick answers!

    @GoToLoop - thanks for a more detailed understanding on the issue causing the problem and letting me know about the p5.js issue repository on GitHub!

    @TfGuy44 - thanks for that line of thinking. I've created a boolean so that some scenes get drawn only once as part of draw() and then draw() just loops doing nothing to keep updating the mouseX and mouseY values.

  • edited January 2016 Answer ✓

    @Endrocil, don't forget p5.js' developer recommended clientX & clientY as workaround. L-)

  • @GoToLoop Yeah, thanks for that too! I'm unsure as of the details of how those work though and I was getting some weird behaviours sometimes while trying to implement the loop() and noLoop() in my code at some points. For whatever reason the boolean to draw the scenes only once seems to be less confusing to me to test/debug.

Sign In or Register to comment.