Breaking out of For Loop when mouse is depressed?


#1

Hi, i’m quite new to coding in general and having a really hard time with this. Currently my loop is running from 0-1000 in a single click, but i need it to break out of the loop when i let go of the mouse. I’m guessing my If to trigger the break is placed incorrectly, but i don’t understand why isPressed isn’t being checked every time the loop executes.

Here’s a simplified sketch of the problem:

var isPressed = false; 

function setup() {
createCanvas(1000, 500);
  background(0);
}

function draw() {
  test();
}


function test()
{
  if (mouseX>0 && mouseX<width && mouseY<height && mouseY>0 && isPressed === true)
  {
      testLoop:
      for (var i=0; i<1000; i++)
      {
        if (isPressed === false)
        {
          break testLoop;
        }
        else 
        {
          print("push");
        }
      
      }   
  }
}


function mousePressed()
  {
    isPressed = true;
  }
  
function mouseReleased()
  {
    isPressed = false;
  }

Here’s my actual code:

//--------------------------------------------
//DRAWS BEATERS ON-SCREEN

function BeaterControl()
{
  
  if (mouseX>0 && mouseX<width && mouseY<height && mouseY>0 && isPressed === true)
  {
      arrayLoop:
      for (var beaterID=0; beaterID<1000; beaterID++)
      {
        if (isPressed === false)
        {
          break arrayLoop;
        }
        else 
        {
          beaterArray[beaterID] = beaterArray.push(new Beater());
        }
      
        console.log(beaterArray[10]);
        //console.log(beaterArray[beaterID].y);
      }
        
      if (beaterSize <= 20)
      {
          beaterSize = (beaterSize+0.2);
      }
       
  }
      
  if (isPressed === false)
  {
    beaterSize = 0;
  }
 
    
}

function Beater()
{
  this.x = mouseX;
  this.y = mouseY;
  this.diameter = beaterSize;
  
  this.beaterFill = fill(250,250,0,250);
  this.beaterDisplay = ellipse(this.x,this.y,10,10);
}

#2

Hi! Mouse and keyboard state is only updated between animation frames, never in the middle of running draw. By default that means that they are updated every 16.66 milliseconds (at 60 fps) (as far as I understand).

What are you trying to achieve with counting up to 1000?

btw. when a mouse is depressed attempts to break out of For Loop often fail. The mouse needs to be very focused to attempt such a escape, so in these situations I always suggest mice to do training first to end the depression. Then, when the moment comes, it will be able to react quickly and sneak out of For Loop without being noticed by those insensitive Loop guards.

:stuck_out_tongue:


#3

i only see a way like

int ccount=0, count=0, countend=100;
boolean reported = false;

void setup() {
  size(200, 200);
  background(200, 200, 0);
  fill(200, 0, 0);
  noStroke();
  frameRate(10);
}

void draw() {
  if (( count <= countend ) && mousePressed ) {
    ellipse(random(200), random(200), 10, 10);
    ccount++;
    print("circles "+ccount+" ");
  }
  println( "count: "+count);
  count++;
  if ( count > countend && !reported ) { 
    println("you made "+ccount+" circles"); 
    reported = true; 
    exit();
  }
}

that gives you 100 loops to draw 100 circles, but only when the mouse is pressed;