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 › keyPressed function
Page Index Toggle Pages: 1
keyPressed function (Read 994 times)
keyPressed function
Jan 20th, 2010, 6:53pm
 
Hi everyone,

let me just apologize beforehand that I am completely new to processing and programming, so I don't have the slightest clue as to how to work this thing...The assignment for my art class is to compose a simple "stream of consciousness." The idea is that a word appears on the initial screen, and when a key is pressed it moves on to the next word associated with it. I have the list of words I want to use, but have no idea how to integrate it into the keyPressed function. Help anyone??
Re: keyPressed function
Reply #1 - Jan 20th, 2010, 9:57pm
 
all you need is an array of strings and a counting variable that is counted up

so our programm would look like this


Code:
String[] textArray = new String[]{ "Text a", "Text b", "Text c" };
PFont font;
int count;

void setup() {
size(200,200);

font = createFont("Arial",14);
textFont(font);
}

void draw() {
background(255);
fill(0);
text(textArray[count],80,100);

}

void keyPressed(){
count++;
if(count==textArray.length)count=0;
}



you could use modulo instead of "if" but this is probably easier to understand...
Re: keyPressed function
Reply #2 - Jan 20th, 2010, 10:29pm
 
Thank you, Cedric!!
That worked perfectly!

Just one more question...if I want each word to appear a little lower than the other one, where would I enter the coordinates for each one?
Re: keyPressed function
Reply #3 - Jan 20th, 2010, 10:30pm
 
what do you think Smiley ?
we have a variable that counts up, this could be used move thetext to a nother coordinate. look at http://processing.org/reference/text_.html to find out how to use it. the example makes it pretty clear i guess.
Re: keyPressed function
Reply #4 - Jan 20th, 2010, 10:40pm
 
I'm not sure what the "s" in "String s" mean Shocked
Re: keyPressed function
Reply #5 - Jan 20th, 2010, 10:41pm
 
can you also explain to me what this line means?

if(count==textArray.length)count=0;
Re: keyPressed function
Reply #6 - Jan 20th, 2010, 11:04pm
 
You should take a look at the Learning section...
Re: keyPressed function
Reply #7 - Jan 20th, 2010, 11:08pm
 
PhilHo is right, you should get used to the syntax and learn how to use the different commands. everything is explained in the reference, also what "s" is in this case.
Re: keyPressed function
Reply #8 - Jan 21st, 2010, 12:58pm
 
alright. I'll do my best. Thanks!
Page Index Toggle Pages: 1