Function which permits leave from loop

Hi

Is there a processing's function which prmits break from a loop, pressing any key of keyboard. I have this code in order to show would it work

import static javax.swing.JOptionPane.*;
bollean out = false;

void setup()
{
  size(400,400);
  smooth();
  // Draw at least one circle, then loop with a 50% chance of repeating.
  do
  {
    ellipse(random(10,width-10), random(10,width-10), 10, 10);
  }
  while (!salir);
}

void keypessed()
 {
    if (key=='p)
    {
       salir = true;
   }
 }

Thanks

Tagged:

Answers

  • not sure if it will work in your case because not sure whether keypessed() is checked during setup() in the first place.

    maybe move the loop to draw

  • edited November 2016 Answer ✓

    Regardless whether Processing already enqueues input events early in setup(), keyPressed() and its siblings will never be automatically called back until draw() runs & returns! [-X

  • Hi:

    I wrote this code using break, but it didn't work... it froze

    import static javax.swing.JOptionPane.*; boolean out = false; void setup() { size(400,400); smooth(); // Draw at least one circle, then loop with a 50% chance of repeating. do { ellipse(random(10,width-10), random(10,width-10), 10, 10); println("Still here..."); } while (!out); } void keyPressed() { switch (key) { case 'p': out = true; showMessageDialog(null, "Just leaving!!!", "Info", INFORMATION_MESSAGE); } }

    Thanks

  • As said, put the loop in draw ()

  • Answer ✓

    The while loop will block the draw function. Draw needs to run freely so to be able to process asynch operations like input actions (correct me if I am wrong @GoToLoop). For an easy check, replace

    println("Still here...");

    with

    println("Still here..."+frameCount);

    You need to reconsider if you need to use a do-while structure.

    Kf

  • edited November 2016 Answer ✓

    (correct me if I am wrong...

    Ummm... The very definition of async is executing more things at the same time. L-)
    Processing got an active Thread just to enqueue all of its input events.
    However, dequeueing is synchronous. And only happens after draw() is finished.
    All events are processed and the queue gets empty.
    Then the current PGraphics canvas is rendered to the PSurface window.
    And the whole cycle repeats. I-)

  • @laimperiestro -- draw() is already a loop, and important stuff happens each time it cycles (draws to the screen, checks input events, etc.). To logically accomplish what you are trying to do, embed if statements within draw().

    Even if you try to work your way around this by setting noLoop() and then writing a loop that calls redraw(), it is still probably a Bad Idea.

  • Hi

    I solved this way, so I took out do-while...

    import static javax.swing.JOptionPane.*; boolean out = false; void setup() { size(400,400); smooth(); // Draw at least one circle, then loop with a 50% chance of repeating. } void draw() { if(out==false) { ellipse(random(10,width-10), random(10,width-10), 10, 10); println("Still here..."+frameCount); } } void keyPressed() { switch (key) { case 'p': out = true; showMessageDialog(null, "Just leaving!!!","Info", INFORMATION_MESSAGE); break; } }

    Thanks

Sign In or Register to comment.