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 & HelpPrograms › Weird Behavior
Page Index Toggle Pages: 1
Weird Behavior (Read 1518 times)
Weird Behavior
Jul 29th, 2008, 4:16pm
 
Hello there... can anyone find out what's wrong with the following code?

The problem with it is that it just doesn't print the 0 when the countdown finishes.

I used the wait method because with the delay method only, the program wasn't displaying some numbers (the weird thing is that it received the correct numbers, but jumped some of then ???? - when it jumps a number, it keeps in the next number more time)

Guess someone can find the problem.

Here is the code:

Code:
PFont fontA; 
int count = 10;

void setup(){
 size(1024, 768);
 background(random(255),random(255), random(255));
 fill(random(255),random(255), random(255));
 fontA = createFont("Palatino Linotype Negreta cursiva", 60);
 textFont(fontA);
}

void draw() {
 countdown(count--);
}


void countdown (int count) {
 textAlign(CENTER, CENTER);
 background(random(255),random(255), random(255));
 fill(random(255),random(255), random(255));
 text(Integer.toString(count),width/2, height/2);
 wait(1000);
 if (count == 0){
   background(random(255),random(255), random(255));
   text("Turn it on",width/2, height/2);
   wait(1000);
 }
}

void wait (int time) {
 noLoop();
 delay(time);
 loop();
}
Re: Weird Behavior
Reply #1 - Jul 29th, 2008, 8:29pm
 
I'm not entirely sure why your code doesn't work, I tried to make it work the way you have it set up with no luck.

I was able to make it work however by getting rid of both your classes ('countdown' and 'wait') and by adding the "PFont = fontA;" statement. I also changed the 'if' statement so that it only displays the text once the count has gone below zero and used a switch so it would only do it once. I then reset the count to 0 so that it would start at -1 after it says "turn it on".

I'm not sure if you intended for it to go below zero.. if not simply change your 'if' statement and your code should work as long as you stop the loop after it displays the text.

I ended up with the following:

Code:
 int count = 10;
PFont fontA;
int sw = 1;

void setup(){
size(1024, 768);
background(random(255),random(255), random(255));
fill(random(255),random(255), random(255));
fontA = createFont("Palatino Linotype Negreta cursiva", 60);
textFont(fontA);
}

void draw() {

textAlign(CENTER, CENTER);
background(random(255),random(255), random(255));
fill(random(255),random(255), random(255));


if (count < 0 && sw == 1){
sw =0;
background(random(255),random(255), random(255));
text("Turn it on",width/2, height/2);
delay(1000);
count = 0;

}


text(Integer.toString(count),width/2, height/2);
delay(1000);
count -=1;
}



Re: Weird Behavior
Reply #2 - Jul 30th, 2008, 11:28am
 
In a recent message, fry said things are displayed only after draw() method returned.
So when you draw something then wait(), it won't be displayed until the end of the wait...
You have to alter the logic somehow to display first, then wait on next iteration.
Re: Weird Behavior
Reply #3 - Jul 30th, 2008, 3:24pm
 
That shall be the problem. I'll try to change it somehow.

Isn't there a method that forces the program to display things before draw() method returned? Without that, u get just tied, having to make seams to make your program works the way u want. If that doesn't exist, doing that would be a good suggestion for the next versions of processing.

Thanks for the help.
Re: Weird Behavior
Reply #4 - Jul 30th, 2008, 3:55pm
 
calling noLoop() and loop() in the same function will cancel each other out. if you want to have very close control over timing, use noLoop() inside setup(), then redraw() whenever you want to run the draw() method right away.
Re: Weird Behavior
Reply #5 - Jul 30th, 2008, 4:47pm
 
But without the noLoop() and loop() in that function, the program doesn't work correctly. I couldn't figure out why, but it happens.

Another thing. When using the redraw() method, how can I make the program run the draw() method n times after a press a key?

I tried this way:
Code:

void keyPressed() {
 if (mode == MODE_INITIALIZATION) {
   mode = MODE_COUNTDOWN;
   for(int n = 10; n>0; n--){
     redraw();
     delay(1000);
   }
 }
}


but it didn't work. Many errors appeared.

Any suggestions?
Re: Weird Behavior
Reply #6 - Jul 30th, 2008, 8:15pm
 
Here is my version:

Code:
PFont fontA;  
int count = 10;

void setup(){
 size(1024, 768);
 background(random(255), random(255), random(255));
 fill(random(255), random(255), random(255));
 fontA = loadFont("AmericanTypewriter-24.vlw");
 textFont(fontA, 60);
 frameRate(1);
}

void draw() {
 countdown(count--);
}


void countdown (int count) {
 textAlign(CENTER, CENTER);
 background(random(255), random(255), random(255));
 fill(random(255), random(255), random(255));
 text(Integer.toString(count), width/2, height/2);
 if (count < 0){
   background(random(255), random(255), random(255));
   text("Turn it on", width/2, height/2);
 }
}


I have a strange problem: I often have a runtime error - "use textFont() before text()" - which might be a loading time issue. After a few tries, it finally runs.
I couldn't reproduce this issue with v. 0135, I had it (say 3 times out of 4) in v.0141 and b.0143.

[EDIT] Related bug: Bug 726 : use textFont() before text(), seems to be fixed with v.0145.
Re: Weird Behavior
Reply #7 - Jul 30th, 2008, 8:23pm
 
Good idea...

Is it possible to change the frame rate back to 60 later in the code?
Re: Weird Behavior
Reply #8 - Jul 31st, 2008, 9:40am
 
A possible way is to put the code changing the display in a test:
if (frameCount % 60 == 0)

Example:

Code:
PFont fontA;  
int count = 10;
int ballSize = 20;
int x = 100, y = 100;
float vx = 1, vy = 1, dvx = 0, dvy = 0;

void setup(){
size(1024, 768);
background(random(255), random(255), random(255));
fill(random(255), random(255), random(255));
fontA = loadFont("AmericanTypewriter-24.vlw");
textFont(fontA, 60);
frameRate(60);
}

void draw() {
drawBall();
if (frameCount % 60 == 0)
countdown(count--);
}


void countdown (int count) {
textAlign(CENTER, CENTER);
background(random(255), random(255), random(255));
fill(random(255), random(255), random(255));
text(Integer.toString(count), width/2, height/2);
if (count < 0){
background(random(255), random(255), random(255));
text("Turn it on", width/2, height/2);
}
}

void drawBall() {
dvx = random(-1.0, 1.0); dvy = random(-1.0, 1.0);
vx += dvx; vy += dvy;
x += vx; y += vy;
if (x < ballSize) { x = ballSize; vx = -vx; }
if (y < ballSize) { y = ballSize; vy = -vy; }
if (x > width - ballSize) { x = width - ballSize; vx = -vx; }
if (y > height - ballSize) { y = height - ballSize; vy = -vy; }
ellipse(x, y, ballSize, ballSize);
}
Page Index Toggle Pages: 1