We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to create his own start, stop, pause controls
Page Index Toggle Pages: 1
how to create his own start, stop, pause controls (Read 733 times)
how to create his own start, stop, pause controls
Jul 29th, 2009, 8:01am
 
hello,

i find this sketch on the forum. i try to implement some controls on it  in order to choose between play, pause(i have one on this sketch),
and stop. More precisely i want to press a key for starting the sketch, another to stop and consequently reset it. I know that i have to do it with  boolean but i don't succeed to do it. Of course the key should be pressed only one time and the screen has to be blank at the beginning.
Please i need help. here the code :

boolean paused = true;

void setup()
{
 size(400,400);
 background(255);
 strokeWeight(2);
 smooth();
}

void draw()
{
 stroke(random(255), random(255), random(255));  
 ellipse(random(0,width),random(0,height),4,4);
}

void keyPressed()
{
 paused = !paused;
 if(paused)
   noLoop();
 else
   loop();
}
Re: how to create his own start, stop, pause controls
Reply #1 - Jul 29th, 2009, 9:05am
 
I don't like using noLoop() as it means you can't be doing anything else in the draw loop.

"paused = !paused" looks a little odd to me too.  Why not just "paused = false"?

Anyway here's some working code:

Code:
// paused = false because I'm assuming you want it to be 'playing' when the sketch starts
// i.e. it is NOT currently paused
boolean paused = false;

void setup() {
size(400,400);
background(255);
strokeWeight(2);
smooth();
}

void draw() {
// Test that it is NOT pausedbefore drawing anything
// Note that (!paused) is equivalent to (paused == false)
if (!paused){
  stroke(random(255), random(255), random(255));  
  ellipse(random(0,width),random(0,height),4,4);
}
}

void keyPressed() {
// if paused == true make it false
if(paused) {
 paused = false;  
}
// otherwise make it true
else {
 paused = true;
}
}
Re: how to create his own start, stop, pause controls
Reply #2 - Jul 31st, 2009, 3:46pm
 
thank you blindfish !  Smiley
Page Index Toggle Pages: 1