# Can I make words float around the screen with the word I type in?

**URL:** <https://discourse.processing.org/t/can-i-make-words-float-around-the-screen-with-the-word-i-type-in/26087>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [December 9, 2020, 11:09am UTC](https://discourse.processing.org/t/can-i-make-words-float-around-the-screen-with-the-word-i-type-in/26087 "2020-12-09T11:09:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quinton](https://avatars.discourse-cdn.com/v4/letter/q/7c8e57/32.png) [@Quinton](https://discourse.processing.org/u/Quinton)\
**Post date:** [December 9, 2020, 11:09am UTC](https://discourse.processing.org/t/can-i-make-words-float-around-the-screen-with-the-word-i-type-in/26087/1 "2020-12-09T11:09:30Z")

</div>

I was trying to make a code that can make the word of sentence I type in would float around the screen.

when I type the letters and hit enter, the typed sentence will bounce around.

It was kinda tricky… since I didnt code enough with string, key etc.

this code does change the letter but its just one letter, and I dont know how to put new strings in it.

should I use array or vector??

```auto
float posx, posy;
float addx, addy;
String s;
void setup(){
  size(1200, 720);
  background(176);
  smooth();
  addx = random(-5, 5);
  addy = random(-5, 5);
  posx = width/2;
  posy = height/2;
}

void draw(){
  background(176);

  textSize(32);
  textWidth(key);
  text(key, posx, posy);

  posx += addx;
  posy += addy;

  if(posx < 0 || posx > width - textWidth(key)){
    addx *= -1;
  }
  if(posy < 16 || posy > height){
    addy *= -1;
  }
  

}

```

---

<div class="post-metadata">

**Author:** ![ErraticGenerator](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/erraticgenerator/32/11304_2.png) [@ErraticGenerator](https://discourse.processing.org/u/ErraticGenerator)\
**Post date:** [December 9, 2020, 8:55pm UTC](https://discourse.processing.org/t/can-i-make-words-float-around-the-screen-with-the-word-i-type-in/26087/2 "2020-12-09T20:55:43Z")

</div>

If you are making this interactive, that means you will not know how long the text will be. So, you want to start with an empty array, and store all the keypresses in another variable, then, when the user hits enter, push the collected string into the array you created earlier.

But you are not only tracking the text but also position, speed, etc. Therefore, it will be better to create an object each time the user hits enter, and store all the data in the object (text, x, y, speed, etc.) you can create this object using class.

Then, you iterate over each object using a for loop.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 9, 2020, 8:56pm UTC](https://discourse.processing.org/t/can-i-make-words-float-around-the-screen-with-the-word-i-type-in/26087/3 "2020-12-09T20:56:30Z")

</div>

here is an example with 2 screens:

one screen where you enter the sentence  
one where the sentence move

```auto

float posx, posy;
float addx, addy;
String s="";

boolean wordHasBeenEntered=false; 

void setup() {
  size(1200, 720);
  background(176);
  smooth();
  addx = random(-5, 5);
  addy = random(-5, 5);
  posx = width/2;
  posy = height/2;
}

void draw() {
  background(176);

  if (wordHasBeenEntered) {

    textSize(32);
    textWidth(key);
    text(s, posx, posy);

    posx += addx;
    posy += addy;

    if (posx < 0 || posx > width - textWidth(key)) {
      addx *= -1;
    }
    if (posy < 16 || posy > height) {
      addy *= -1;
    }
  }

  //-----------------------------------

  else {
    //
    text("Please enter your word: \n"
      +s, posx, posy);
  }//else 
  //
}

void keyPressed() {
  if (wordHasBeenEntered) 
    return; //leave 
  if (key==CODED)
    return; //leave  
  if (key == ENTER||key==RETURN) {
    wordHasBeenEntered=true;
    return;
  }
  s+=key;
}
//

```

When you want to split the sentences to words, use split() command to make that array arr1. In keyPressed, section RETURN

declare arrays of the length of arr1 named x,y,addx,addy

fill them with random values

apply them in a for-loop in draw() in section `if (wordHasBeenEntered) {`

---

<div class="post-metadata">

**Author:** ![Quinton](https://avatars.discourse-cdn.com/v4/letter/q/7c8e57/32.png) [@Quinton](https://discourse.processing.org/u/Quinton)\
**Post date:** [December 11, 2020, 2:36pm UTC](https://discourse.processing.org/t/can-i-make-words-float-around-the-screen-with-the-word-i-type-in/26087/4 "2020-12-11T14:36:50Z")

</div>

wow this really helped!! thank you very much!!!
