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 › Help Needed with Delaying
Page Index Toggle Pages: 1
Help Needed with Delaying (Read 221 times)
Help Needed with Delaying
Jan 26th, 2009, 12:10pm
 
Hello,

I'm a newbie in desperate need of help. I built a programme that 'mirrors' the letters on a normal qwerty keyboard.
It is an installation where visitors should type on a poiuyt keyboard. They should type various words and if they typed all of them correctly the program rewards them with a:
"You taught yourself how to type"

I would like to have this line at the screen for about 3 seconds and then the program should return to the beginning.

I cannot get this to work.

The delay already occurs with the last word (or letter in the easy version of the program) and then it skips the line totally before returning to the beginning. Can somebody please help me with this?
I've been at it for quite a while now and my presentation is only one week ahead, so I'm getting worried.

Below you'll find my code.

I'll be eternally grateful, kYra

code:

char [] correct = {
 'q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z',
'x','c','v','b','n','m'};
char [] reversed = {
 'p','o','i','u','y','t','r','e','w','q','l','k','j','h','g','f','d','s','a','m',
'n','b','v','c','x','z'};

// I would like to have the visitor type words but to
// make the program easier to work in, i use only letters
// and will change the array when it is completely working
// the letters p, d, e are on a 'normal'
// keyboard q, j , i
String [] words = {"p", "d", "e"};
// String [] words = {"arbitrary", "control", "adapting", "typing", "mistake", "culture", "form", ""};


PFont font;
String letters = "";
int counter;
int cursorBlinkDelay = 20;
float y;
 int a;

void setup() {
 size(1440, 900);
 font = loadFont("APCCourier-Bold-48.vlw");
 textFont(font);
 stroke(0);
 fill(0);
}

void draw() {
 background(255);
 text(words[a], 0,height/4);
     
 
 int lY = height/2;
 line(0,lY, width, lY);
   line(0,lY+2, width, lY+2);
       line(0,lY+4, width, lY+4);
       
 y  = 0.75 * height;
 float cursorPosition = textWidth(letters);
 
  if (counter++ > cursorBlinkDelay)
  text("_", cursorPosition, y);
 
    if (counter > 2*cursorBlinkDelay) counter = 0;
 text(letters, 0, y);
 
 if(letters.equals(words[a]))
   a = a+1;
   
   if(a == words.length) {
 text("You taught yourself how to type!!!",  0,height/4);
 delay(3000);
 // The delay seems to be the problem, the programme delays allready
// after the last letter is exposed I would like to have the above
// text ("You taught...") to be on screen for about a second or 3
// and then the programme should return to the beginning.
 
 a = 0;
   }
 
}


void keyPressed() {
 if (key == BACKSPACE) { // Backspace
   if (letters.length() > 0) {
     letters = letters.substring(0, letters.length()-1);
   }
 }
 else if (textWidth(letters+key) < width){
   char nieuweLetter = omdraaien(key);
   letters = letters+nieuweLetter;
 }
}


char omdraaien(char letter) {
 char letterUit = ' ';

 for(int i=0; i<correct.length; i++) {
   if(letter == correct[i]) {
     letterUit = reversed[i];
   }
 }    
 return letterUit;
}
Re: Help Needed with Delaying
Reply #1 - Jan 26th, 2009, 12:46pm
 
Wow, it is hard on an Azerty keyboard! Wink

Here is a possible simple solution. Showing only the relevant changes:
Code:
boolean bShowingCongrat;

void setup() {
//...
}
void draw() {
if (bShowingCongrat) delay(3000);
bShowingCongrat = false;
background(255);
//...
if(a == words.length) {
text("You taught yourself how to type!!!", 0,height/4);
bShowingCongrat = true;
a = 0;
}
}

You need the two-time setting (displaying then delaying) because display is updated only when ending draw().

Another way is to avoid delay() entirely, eg. if you want to keep changing display while showing the message. In this case, you watch time or frame count, and stop displaying the message when the delay is reached.
Re: Help Needed with Delaying
Reply #2 - Jan 26th, 2009, 12:56pm
 
thank you thank you thank you....

I needed to add a background(255), to wipe the last letter, but now it works like I want to!

So happy, thank you.
Page Index Toggle Pages: 1