Multiple keyPressed to change text message

edited December 2016 in Questions about Code

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.

    String[] quotes = { ... };
    int which_quote = 0;
    
    void draw(){
      background(0);
      text( quotes[which_quote], 20,20);
    }
    
    void keyPressed(){
      if(keyCode == 'c'){
        which_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;

  • edited December 2016
    // forum.Processing.org/two/discussion/19964/
    // multiple-keypressed-to-change-text-message#Item_5
    
    // GoToLoop (2016-Dec-29)
    
    static final color BG = #302F2F, FG = #FFE224;
    static final int OFFSET = 50, TXT_SIZE = 020;
    
    static final String[] QUOTES = {
      "Everyday", 
      "Today", 
      "Yesterday",
      "Tomorrow"
    };
    
    int idx;
    
    void setup() {
      size(600, 200);
      smooth(3);
      noLoop();
    
      fill(FG);
      blendMode(REPLACE);
    
      textAlign(LEFT, CENTER);
      textSize(TXT_SIZE);
    }
    
    void draw() {
      background(BG);
      text(QUOTES[idx], OFFSET, height>>1);
      print(idx, TAB);
    }
    
    void keyPressed() {
      final int k = keyCode;
      if (k == 'C' | k == ' ')  idx = (idx + 1) % QUOTES.length;
      redraw();
    }
    
  • @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

  • edited December 2016

    Perhaps increases performance given it turns alpha off.

    Turning alpha off may not increase performance significantly. The differences will not be noticeable.
    P.S. This post has been updated multiple times already.

  • edited January 2017

    Many thanks for your great help, it work like I hoped it would! Thanks! :)

Sign In or Register to comment.