How to make something inside a loop happen once?

Hey guys.

In this code I’d like the words to be randomly generated just once and not over and over again, while still keeping it bouncing from left to right.

I’m new at processing so help is much appreciated!

float textX; // x position
float xspeed = 0.15; 
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"}; // string med de 3 sætninger
PFont font1;




void setup() {
  println(words.length);
  size (700, 1000);
  font1 = loadFont ("Akkurat-Bold-18.vlw");
  textAlign(LEFT);
  textX = 0;
  frameRate(20);
}

void draw() {
  background (0);
    textFont(font1, 30); 
  for (int y = 25; y < height; y = y + 30) { 
      fill(255);
      text(words [int(random(words.length))], textX, y); 


      textX = textX + xspeed; 
      

      
      if (textX > width) {
        xspeed = -0.15; 
      }
      
      if (textX < 0) { 
        xspeed = 0.15;
      }
    }
}
1 Like

store this in a Variable in setup()

use this variable in draw()

Declare that int variable before setup ()

Chrisir

1 Like

Hi Chris

I can’t figure it out

I did this:

before setup:
int a;

in setup():
a = words.length;

in draw():
text(words (random(a)), textX, y);

What am I missing here?



// string med de 3 sætninger
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"};
// its index 
int a;
// text position X
int textX=0;

void setup() {
  size(800, 700);
  a = int(random(words.length));
  background(0);
}

void draw() {
  background(0);
  text(words[a], textX, 100);
  textX = textX + 5;
}

when you are done with one word (it left the screen) restart textX and a

Hi Chris

I’m getting this weird effect instead

//float textX; // x position
float xspeed = 0.15; // fart som den bevæger sig på x-aksen
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"}; // string med de 3 sætninger
PFont font1;
int a;
int textX=0;





void setup() {
  println(words.length);
  size (700, 1000);
  font1 = loadFont ("Akkurat-Bold-18.vlw");
  textAlign(LEFT);
  textX = 0;
  frameRate(20);
fill( random(255), random(255), random(255), random(255)); 
a = int(random(words.length));
  

}

void draw() {
  
  background (0);
    textFont(font1, 30); // loader skrifttype
  for (int y = 25; y < height; y = y + 30) { // for loop på y-aksen
      text(words [a], textX, y); // String er genereret random
      textX = textX +5;



      //textX = textX + xspeed; // bevæg med xspeed til højre hvert loop
      
// bounce frem og tilbage
      
      if (textX > width) {
        xspeed = -0.15; //hvis tekst når width skal den gå modsat vej
      }
      
      if (textX < 0) { // hvis tekst når 0 skal den gå tilbage igen
        xspeed = 0.15;
      }
    }
//






}
1 Like

Can you please describe the effect

I want the code to randomly select words from the string just once in the start, so it looks like this:

vinterjazz
01-24 feb. 2019
vinterjazz
www.jazz.dk
www.jazz.dk
01-24 feb. 2019
vinterjazz
etc

right now it keeps on generating new ones

Hi @Jacobo,

For a cleaner approach I would suggest:

  • to divide the height of your canvas by the desired number of lines.
  • to make a list of all the words picked randomly in setup()
  • that list will have he same size as the desired number of lines
  • to compute the length of the longest word for a cleaner edge detection

Regarding your issue, you just have to multiply the speed by -1 when the location of your text reaches an edge.

Here’s an example in Python Mode.

nLines, speed, location = 32, 1.8, 0
words = ["vinterjazz", "01-24 feb. 2019", "www.jazz.dk"]
wordsList = [words[int(random(len(words)))] for i in range(nLines)]

def setup():
    size(700, 1000, P2D)
    fill(255)
    
    global step, longestWord
    step = height / nLines
    longestWord = max([textWidth(w) for w in words])
    
def draw():
    background(0)
    
    global location, speed
    location += speed
    
    for i in range(nLines): text(wordsList[i], location, (i+1) * step)
    if location + longestWord >= width or location <= 0: speed *= -1

And here below my failed attempt in Java, let’s hope someone will find some time to fix it.

float location = 0;
float speed = 1.8;
int nLines = 32;
float step = 1000 / nLines;
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"};
float longestWord = 0.0;
String [] wordsList;



void setup() {
  size(700, 1000, P2D);
  fill(255);
 
  
  for (int i = 0; i < words.length; i++) { 
    longestWord = max(longestWord, textWidth(words[i]));
  }
  
  for (int i = 0; i < nLines; i++) { 
    wordsList[i] = words[int(random(words.length))]; 
  }
}

void draw() {
  background(0);
  
  location += speed;
  
  for (int i = 0; i < nLines; i++) {
    text(wordsList[i], location, (i+1) * step);
  }
  
  if (location + longestWord >= width || location <= 0) {
    speed *= -1;
  }
}
1 Like

here is a solution that has one line only that’s chosen once and for all (not what you want, I know)


//float textX; // x position
float xspeed = 2.15; // fart som den bevæger sig på x-aksen
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"}; // string med de 3 sætninger
PFont font1;
int a;
float textX=10;

void setup() {
  println(words.length);
  size (700, 1000);
  font1 = createFont ("Akkurat-Bold-18.vlw", 30);
  textAlign(LEFT);
  textX = 0;
  frameRate(20);
  fill( random(100, 255), random(100, 255), random(100, 255) ); 
  a = int(random(words.length));
}

void draw() {
  background (0);

  textFont(font1, 30); // loader skrifttype

  text(words[a], textX, 200); // String er genereret random

  textX = textX + xspeed; // bevæg med xspeed til højre hvert loop

  // bounce frem og tilbage
  if (textX > width-textWidth(words[a])) {
    xspeed = -abs(xspeed); //hvis tekst når width skal den gå modsat vej
  }

  if (textX < 0) { // hvis tekst når 0 skal den gå tilbage igen
    xspeed = abs(xspeed);
  }

  //
}

here a version like from solub avbove in java


float location = 0;
float speed = 1.8;
int nLines = 32;
float step = 1000 / nLines;
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"};
float longestWord = 0.0;
String [] wordsList = new String[nLines];

void setup() {
  size(700, 1000, P2D);
  fill(255);

  for (int i = 0; i < words.length; i++) { 
    longestWord = max(longestWord, textWidth(words[i]));
  }

  for (int i = 0; i < nLines; i++) { 
    wordsList[i] = words[int(random(words.length))];
  }
}// function 

void draw() {
  background(0);

  location += speed;

  for (int i = 0; i < nLines; i++) {
    text(wordsList[i], location, (i+1) * step);
  }

  if (location + longestWord >= width || location <= 0) {
    speed *= -1;
  }
}// function

similar but with different colors and different x pos (and speed) for each line:

int nLines = 32;
float step = 1000 / nLines;
String [] words = {"vinterjazz", "01-24 feb. 2019", "www.jazz.dk"};
float longestWord = 0.0;

String [] wordsList = new String[nLines];
float[] location =  new float[nLines];
float[] speed    = new float[nLines]; // 1.8
color[] col      = new color[nLines]; 

void setup() {
  size(600, 1000, P2D);

  for (int i = 0; i < words.length; i++) { 
    longestWord = max(longestWord, textWidth(words[i]));
  }

  for (int i = 0; i < nLines; i++) { 
    wordsList[i] = words[int(random(words.length))];
    location[i]  = i*3.4;
    speed[i]     = 1.8;
    col[i]       = color ( random(100, 255), random(100, 255), random(100, 255));
  }
}// function 

void draw() {
  background(0);

  for (int i = 0; i < nLines; i++) {

    fill ( col [i] ); 
    text(wordsList[i], 
      location[i], (i+1) * step);

    location[i] += speed[i];

    if (location[i] + longestWord >= width || location[i] <= 0) {
      speed[i] *= -1;
    }
  }//for
}// function
1 Like