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 › restarting a sketch
Page Index Toggle Pages: 1
restarting a sketch (Read 381 times)
restarting a sketch
Nov 11th, 2008, 11:59am
 
i was wondering if anyone could give me some advice on how i could "restart" a sketch that draws triangles on the screen.

the triangles are drawn by receiving numbers via osc from PureData. i left the background(); setting as a comment so that each time a triangle changes a trace is left on the canvas.

void draw() {
//background(0);
 stroke(0, 250, 0);
 triangle(bX + 100, bY + 100, bX+10 / 3, 40 + 100, 80 + 90, 90+10);
 triangle(bY, bX, bX+10, 40, 80, 90);
 triangle(bY, bX - 90, bY+100, 180, 140, 190);
 triangle(bY+ 300, bX+40, bY+100, 180, bX - 40, 190);

the bY and bX are ints received from PD.

after a while the entire screen is almost totally green (duh) but i figured there might be a way to either make the triangles fade away, or restart the sketch every 10 seconds?

nota bene i am a beginner. i never used any programming language before.

thanks!
.klive
Re: restarting a sketch
Reply #1 - Nov 11th, 2008, 1:23pm
 
i just started learning processing, but this piece of code seems to reset the sketch:

Code:

int count = 0;

void setup() {
size(800, 450);
frameRate(30);
background(0);
}


void draw() {
count++;
if(count > 300) {
count = 0;
background(0);
}
stroke(255);
triangle(mouseX, mouseY,
mouseX + 40, mouseY+ 40,
mouseX + 40, mouseY - 40);
}



This would clear the sketch every ~10 seconds assuming the frameRate is reached. It's probably 'neater' to do this timer based.
Or by the ammount of triangles on the screen?

HTH.
Re: restarting a sketch
Reply #2 - Nov 11th, 2008, 6:23pm
 
Another way is to add this in your draw:

Code:

 fill(0,5);
 rect(0,0,width,height);


It just draws a rectangle the size of the window at a specified color and transparency, in this case black (0) with an alpha transparency of 2. Play with the alpha to change how quickly your triangles fade, or write something that times the draws of the rectangles, like drawing them every other frame or every ten frames, etc.

Code:

int count = 0;

void setup() {
 size(800, 450);
 frameRate(30);
 background(0);
}


void draw() {

 fill(0,2);
 rect(0,0,width,height);
 
 stroke(255);
 triangle(mouseX, mouseY,  
     mouseX + 40, mouseY+ 40,  
     mouseX + 40, mouseY - 40);
}
Re: restarting a sketch
Reply #3 - Nov 11th, 2008, 11:36pm
 
thank you so much !

the counter works really well and my grade just went up a few digits!

.klive
Re: restarting a sketch
Reply #4 - Nov 12th, 2008, 12:01am
 
processing also includes time based functions e.g.:
http://processing.org/reference/millis_.html
so you could tie your reset to an actual time based value using an if statement. i.e. "if 10000 milliseconds have passed, then draw a black rectangle".
Page Index Toggle Pages: 1