sw01
Full Member
Offline
Posts: 175
Re: how to : char array
Reply #3 - Nov 29th , 2008, 3:11am
I see, so you don't want the character to appear when you hit a letter? The problem is that the program has no way to distinguish the input between "Typing" and "hitting any key." Either way, keyPressed() event gets called. Meanwhile, the program needs to keep running, either to display something, or wait for an event to occur. How would you like the program to know when to display your sentence? Should it be some specified amount of time after the user stops typing, like: String sentence2; int countdown; PFont fontA; void setup() { countdown = 50; sentence2 = ""; size (500,500); noStroke(); background(0); fontA = loadFont("BerlinSansFB-Reg-48.vlw"); textAlign(CENTER); textFont(fontA, 10); } void draw () { background(0); if( countdown <= 0){ countdown = 0; char[] sentence = sentence2.toCharArray(); int x=20, y= 20; for(int j=0; j< sentence2.length(); j++){ text(sentence[j], x*j, y*j); } println("" + x + " " + y); } countdown--; println(countdown); } void keyPressed() { if (countdown <= 0){ sentence2 = ""; } countdown = 50; sentence2 = sentence2 + key; } So, if the user stops typing, it'll display what's typed after a while. How's this? Also, if you want multiple sentences to be stored, you need an array of String rather than char[]. I recommend using ArrayList rather than array for this purpose. ==UPDATE=== I reread your post and understand what you mean now - you want to wait until the user hits the return key! so here is the correct code: String sentence2; int countdown; PFont fontA; void setup() { countdown = 50; sentence2 = ""; size (500,500); noStroke(); background(0); fontA = loadFont("BerlinSansFB-Reg-48.vlw"); textAlign(CENTER); textFont(fontA, 10); } void draw () { background(0); if( countdown <= 0){ countdown = 0; char[] sentence = sentence2.toCharArray(); int x=20, y= 20; for(int j=0; j< sentence2.length(); j++){ text(sentence[j], x*j, y*j); } println("" + x + " " + y); } //countdown--; println(countdown); } void keyPressed() { if (key == '\n'){ countdown = 0; }else{ if (countdown == 0){ sentence2 = ""; } countdown = 50; sentence2 = sentence2 + key; } }