Change strings individually

edited December 2013 in How To...

Hi all,

I'm a bit stuck with trying to achieve something. I've made a 'program' which draws three sentences, randomly picked from a string. On each mousePressed all the sentences are redrawn. But what I want to achieve is to change each sentence individually. So if you click on the first one, that one changes (and the other two don't). Same for the second and third. I know it must be something like if (mouseX >0 && mouseX<100 && mouseY>0 && mouseY<100) but I can't manage to make it work.

Here's my code:

String [] eersteregel = {
  "dit stille weten", "in alle eenvoud", "vroege dageraad", "niets is er nodig", "ik ben zo iemand", "waar te beginnen"
};

String [] tweederegel = {
  "dat jijzelf die ruimte bent", "wakker te zijn vanochtend", "kijken naar wat je lief is", "slechts eindeloze ruimte", "die stil de zon ziet opgaan", "cute", "crying", "de dag ligt nu nog open"
};

String [] derderegel = {
  "waar alles plaatsvindt", "de dag beginnen", "meer is niet nodig", "en deze stilte", "en zich verwondert", "zo veelbelovend",
};


PFont georgia;


void setup() {
  size(400, 400);
  background(255);
  georgia = createFont("Georgia", 24); 
  noLoop();
}

void draw() {
  background(255);

  textFont(georgia, 24); 
  fill(0);

  String word1 = eersteregel[int(random(eersteregel.length))];
  text(word1, 50, 100);
  String word2 = tweederegel[int(random(tweederegel.length))];
  text(word2, 50, 150);
  String word3 = derderegel[int(random(derderegel.length))];
  text(word3, 50, 200);
}

void mousePressed() {
  redraw();
}

Has anyone a clue how to solve this? Any help is appreciated. Regards, roos laan

Answers

  • _vk_vk
    edited December 2013

    This would be easier with a class to wrap together the strings, the positions and the behaviour. Have you seen this tutorial? But with no classes it would look like this:

    String [] eersteregel = {
      "dit stille weten", "in alle eenvoud", "vroege dageraad", "niets is er nodig", "ik ben zo iemand", "waar te beginnen"
    };
    
    String [] tweederegel = {
      "dat jijzelf die ruimte bent", "wakker te zijn vanochtend", "kijken naar wat je lief is", "slechts eindeloze ruimte", "die stil de zon ziet opgaan", "cute", "crying", "de dag ligt nu nog open"
      };
    
    String [] derderegel = {
      "waar alles plaatsvindt", "de dag beginnen", "meer is niet nodig", "en deze stilte", "en zich verwondert", "zo veelbelovend",
    };
    
     int firstLine = 100, secondLine = 150, thirdLine = 200;
     int x = 50;
     String word1, word2, word3;
     int index1, index2, index3;
    
    PFont georgia;
    
    
    void setup() {
      size(400, 400);
      background(255);
      georgia = createFont("Georgia", 24); 
      noLoop();
      index1 = int(random(eersteregel.length));
      index2 = int(random(tweederegel.length));
      index3 = int(random(derderegel.length));
    }
    
    void draw() {
      background(255);
    
      textFont(georgia, 24); 
      fill(0);
    
      word1 = eersteregel[index1];
      text(word1, x, firstLine);
    
      word2 = tweederegel[index2];
      text(word2, x, secondLine);
    
      word3 = derderegel[index3];
      text(word3, x, thirdLine);
    }
    
     void mousePressed() {
    
          if(mouseX > x && mouseX < x + textWidth(word1) && 
            mouseY > firstLine - 24 && mouseY < firstLine ) {
               index1 = int(random(eersteregel.length));
               }
    
          if(mouseX > x && mouseX < x + textWidth(word2) && 
            mouseY > secondLine - 24 && mouseY < secondLine ) {
               index2 = int(random(tweederegel.length));
               }
    
          if(mouseX > x && mouseX < x + textWidth(word3) && 
            mouseY > thirdLine - 24 && mouseY < thirdLine ) {
               index3 = int(random(derderegel.length));
               }
    
      redraw();
    }
    
Sign In or Register to comment.