We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello
I am trying to write a code that displays a different inspiration quote every time the letter c is pressed. In plain English, what I am hoping to achieve is:
Default message displayed.
Letter c pressed: Display "Every day is a new chance." in stead of the previous quote.
Letter c pressed: Display "Fear killed more dreams than failure ever will." in stead of the previous quote.
Letter c pressed: Display "Take a chance." in stead of the previous quote.
I came across another discussion thread discussing using the use of the same key, and I have tried to adopt the code. It works good for using the same key twice, and I'd like to know if I can use it for more than two keyPressed given it's based on a boolean. https://processing.org/discourse/beta/num_1209751117.html
Another issue is, the transition is not so smooth/reliable as the sentences seem to be flashing before settling. I tried having the background before the if-statements in draw() but it made the text appear very faint.
This is my adapted code:
PFont f;
boolean Cpressed=false;
void setup()
{
size(600, 200);
//printArray(PFont.list());
f = createFont("Fox&Cat",20);
textFont(f);
textAlign(LEFT,CENTER);
smooth();
fill(#FFE224);
textLeading(50);
background(#302F2F);
}
void draw()
{
if (Cpressed == false){
if (keyPressed){
if (key == 'c' )
{
Cpressed = true;
background(#302F2F);
String wordText = "Every day is a new chance.";
text(wordText, 50, height/2);
}
}
}
else if (Cpressed == true )
{
if (keyPressed) {
if (key == 'c'){
Cpressed =false;
background(#302F2F);
String wordText = "Fear killed more dreams than failure ever will.";
text(wordText, 50, height/2);
}
}
}
}
Tried having if-statements within each other, but I can only seem to make it work pressing different letters:
PFont f;
void setup() {
size(600, 200);
//printArray(PFont.list());
f = createFont("Fox&Cat",20);
textFont(f);
textAlign(LEFT,CENTER);
smooth();
fill(#FFE224);
textLeading(50);
background(#302F2F);
}
void draw() {
if (keyPressed){
if (key == 'c' ) {
background(#302F2F);
String wordText = "Every day is a new chance.";
text(wordText, 50, height/2);
}
if (key == 'b') {
background(#302F2F);
String wordText = "Fear killed more dreams than failure ever will.";
text(wordText, 50, height/2);
}
}
}
I am quite new to processing and will greatly appreciate any ideas or pointing in the right direction. Many thanks for your kind help and your time!
Answers
Store all your Strings into Arrays.
This tutorial might help - https://processing.org/tutorials/arrays/.
Thanks for your advice Lord_of_the_Galaxy, that will definitely help clean up the main bit of text. How would I deal with the multiple keyPressed issue though? Many thanks!
You should have an Array of Strings to store your quotes, and an integer index into that array that determines which quote should currently be seen. Then, when the c key is pressed, you simple increase the value of your index to display the next quote.
And of course, you must also check if the index goes beyond the maximum limit with this -
which_quote = (which_quote >= quotes.length)? 0 : which_quote;
@GoToLoop Why exactly are you using
blendMode(REPLACE)
? AFAIK it shouldn't really affect the program.Dunno. Perhaps increases performance given it turns alpha off. :P
Turning alpha off may not increase performance significantly. The differences will not be noticeable.
P.S. This post has been updated multiple times already.
Many thanks for your great help, it work like I hoped it would! Thanks! :)