Different behaviours for text in code

edited March 2016 in Questions about Code

I have text which I'd like to display on random X-Y co-ords and fade out. This text is randomly pulled from a string array. This words correctly using an if loop for decrementing alpha over time.

The simple code for the string "this text will be instructions" takes on the 'attributes' though and instead of simply displaying for x milliseconds at set co-ords, also fades and is positioned randomly. I know the problem is with my use of loops but can't for the life of me see where / why.

Thank you in advance.

if (millis() < 5000) {
    text("this text will be instructions",width/3,height/3);
  }
  else if (millis() - lastTime >= timeToDisplay) // if statement to allow text to fade in and out.  
  {
    count = int(random(myArray.length)); // use length of array to get word random within its length (size)
    textSize(random(35, 50)); // random sized text - set min / max values
    randomX = random(100, 500); // random placement of text
    randomY = random(100, 500);
    alphaSetting = 255; // set alpha setting of 255 = fully 'there'
    lastTime = millis();
  }
  fill(white, alphaSetting); // white text
  text(myArray[count], randomX, randomY); //  chose word from myArray - an array of the words from 
  alphaSetting -= a; // fade out of text by decrememtning alpha

  cam.endHUD();

Answers

  • int lastTime=millis(); 
    int timeToDisplay=2100;
    int count; 
    
    String [] myArray= {
      "polar", "bear", "cat", "ape"
    };
    
    float randomX, randomY; 
    
    float alphaSetting; 
    float a = 1; // speed of fading  
    color white=color(255);
    
    void setup() {
      size(1100, 900);
      background(111);
    }
    
    void draw() {
    
      background(111); 
    
      if (millis() < 5000) {
    
        text("this text will be instructions", width/3, height/3);
      } else if (millis() - lastTime >= timeToDisplay) // if statement to allow text to fade in and out.
    
      {
        count = int(random(myArray.length)); // use length of array to get word random within its length (size)
        textSize(random(35, 50)); // random sized text - set min / max values
    
        randomX = random(100, 500);  // random placement of text
        randomY = random(100, 500);
    
        alphaSetting = 255; // set alpha setting of 255 = fully 'there'
    
        lastTime = millis();
      }//else if
      fill(white, alphaSetting); // white text
      text(myArray[count], randomX, randomY); //  chose word from myArray - an array of the words from 
      alphaSetting -= a; // fade out of text by decrementing alpha
    
      //cam.endHUD();
    }
    
  • you don't post the entire sketch / a mcve

    so it's hard for us because we have to rewrite it all

    maybe it doesn't work because you don't have that:

    background(111); 
    
  • edited March 2016

    Thank you @chrisir for your reply. When I run your code, I get the random string from myArray but don't see "this text will be instructions" anywhere.

    What is an mcve?

    I have background in my code. Here it is in full.

        /*
        Code to display a number of wireframe (and a single textured) spheres on the screen, playing ambient music in the background and displaying strings on the screen in random places. The strings are taken from myText before being passed into an array (myArray).
         */
    
        // import PeasyCam library to enable camera movements
        import peasy.*;
        PeasyCam cam;
        // import minim to play sound
        import ddf.minim.*;
        AudioPlayer player;
        Minim minim;//audio context
    
        //set global variables
        color gray = color(131, 131, 131);
        color white = color(255, 255, 255);
        PShape ball1, ball2, ball3, ball4, ball5, ball6, ball7, ball8, ball9, ball10;
        PImage img, moon;
        PFont font;
        int count; 
        int timeToDisplay = 5000; // time between displaying text
        int lastTime; // When the current image was first displayed
        int camMin = 75;
        int camMax = 30000;
        int sphereSize = 25;
        int lowSphereDetail = 15;
        int highSphereDetail = 50;
        int maxSphereDetail = 140;
        float randomX, randomY;
        float alphaSetting = 255;
        int a = 1; //speed of fade. Smaller number = lower decrementing of alpha setting so a slower fade
    
        String myText = "my spirit, too weak, mortality, weighs, heavily on me, like unwilling sleep, each imagined pinnacle, steep, goldike hardship, tells, I must die, sick eagle looking, at the sky, yet tis a gentle luxuary, to weep, I have not, cloudy winds, keep fresh, opening of the morning's eye, such dim, conceived gloried, bring round, heard an undescribable feud, so do these wonders, a most dizzy pain, mingles Grecian grandeur, rude wasting of old time, with a billowy main, sun, a shadow of magnitude";
        // Split automatically takes the string myText and passes it into the array, splitting it according to the delimeter. The second arguement in split() is the delimiter. Words between commas are a single entry into the array myArray. 
        String [] myArray = split (myText, ",");  
    
        void setup() {
          size(1000, 1000, P3D); // set size of sketch - the third variable P3D gives it a 3rd dimansion
          smooth();
          font = createFont("Verdana", 1, true); // create the font to be used for text. Although the size used will be random. 
          noFill();
          lastTime = millis();
          img = loadImage("nightsky.jpg");    // load images in preparation for background image
          moon = loadImage("moon.jpg");
          noFill(); // keep spheres as wireframe
    
          // create spheres of varying sizes and adjust detail from default 30 for aestheitc reasons. 
          stroke(gray);
          sphereDetail(maxSphereDetail);
          ball9 = createShape(SPHERE, sphereSize*256);
          sphereDetail(lowSphereDetail);
          ball8 = createShape(SPHERE, sphereSize*128);
          ball7 = createShape(SPHERE, sphereSize*64);
          ball6 = createShape(SPHERE, sphereSize*32);
          ball5 = createShape(SPHERE, sphereSize*16);
          ball4 = createShape(SPHERE, sphereSize*8);
          ball3 = createShape(SPHERE, sphereSize*4);
          ball2 = createShape(SPHERE, sphereSize*2);
          noStroke(); // remove stroke from smallest sphere for best texture effect
          sphereDetail(highSphereDetail); // very smooth small sphere even when zoomed in
          ball1 = createShape(SPHERE, sphereSize);
          ball1.setTexture(moon); // give smallest sphere a texture
    
          minim = new Minim(this); // load sound.mp3 to play as ambient music 
          player = minim.loadFile("sound.mp3", 2048);
          player.play();
    
          cam = new PeasyCam(this, 1000); // set camera's initial position and min max camera position
          cam.setMinimumDistance(camMin);
          cam.setMaximumDistance(camMax);
        }
    
        void draw()
        {
          background(img); // background in draw not setup so drawn on loop and new sphere replaces old - load image variable "nightsky.jpg");
          rotateX (-0.9); // slow roatation slightly after mouse has stopped being used. 
          rotateY(-0.9);
          lights(); // lighting is necessary for the texture to show on the innermost sphere. Also gives a fading in and out effect on the text if the camera is being moved
          // draw spheres
          shape(ball1);  
          shape(ball3);  
          shape(ball4);  
          shape(ball5);  
          shape(ball6);  
          shape(ball7);  
          shape(ball8);  
          shape(ball9);
          translate(width/2, height/2, 300); // set position of spheres (centre of screen)
    
          cam.beginHUD(); // end peasy cam so that words appear in the screen and aren't changed by 'camera' movement and position
         if (millis() < 5000) {
        text("this text will be instructions",width/3,height/3);
          if (millis() - lastTime >= timeToDisplay) // if statement to allow text to fade in and out  
          {
            count = int(random(myArray.length)); // use length of array to get word random within its length (size)
            textSize(random(35, 50)); // random sized text - set min / max values
            randomX = random(100, 500); // random placement of text
            randomY = random(100, 500);
            alphaSetting = 255; // set alpha setting of 255 = fully 'there'
            lastTime = millis();
          }
          fill(white, alphaSetting); // white text
          text(myArray[count], randomX, randomY); //  chose word from myArray - an array of the words from 
          alphaSetting -= a; // fade out of text by decrememtning alpha
    
          cam.endHUD();
        }
    
        void stop() // end sound as can cause an error if continued
        {
          player.close();
          minim.stop();
          super.stop();
        }
    
  • the code you shared did not seem to compile so i did some minor changes and it worked, plus you were missing some brackets so im not sure if you where having this issues because the code you posted as is or if you alter the code prior to posting

    also i noticed that at the vary end you have a function that i do not see being used at all though out your draw or setup so was wondering why the implementation ?>

    /*
    Code to display a number of wireframe (and a single textured) spheres on the screen, playing ambient music in the background and displaying strings on the screen in random places. The strings are taken from myText before being passed into an array (myArray).
     */
    
    // import PeasyCam library to enable camera movements
    import peasy.*;
    PeasyCam cam;
    // import minim to play sound
    import ddf.minim.*;
    AudioPlayer player;
    Minim minim;//audio context
    
    //set global variables
    color gray = color(131, 131, 131);
    color white = color(255, 255, 255);
    PShape ball1, ball2, ball3, ball4, ball5, ball6, ball7, ball8, ball9, ball10;
    PImage img, moon;
    PFont font;
    int count; 
    int timeToDisplay = 5000; // time between displaying text
    int lastTime; // When the current image was first displayed
    int camMin = 75;
    int camMax = 30000;
    int sphereSize = 25;
    int lowSphereDetail = 15;
    int highSphereDetail = 50;
    int maxSphereDetail = 140;
    float randomX, randomY;
    float alphaSetting = 255;
    int a = 1; //speed of fade. Smaller number = lower decrementing of alpha setting so a slower fade
    
    String myText = "my spirit, too weak, mortality, weighs, heavily on me, like unwilling sleep, each imagined pinnacle, steep, goldike hardship, tells, I must die, sick eagle looking, at the sky, yet tis a gentle luxuary, to weep, I have not, cloudy winds, keep fresh, opening of the morning's eye, such dim, conceived gloried, bring round, heard an undescribable feud, so do these wonders, a most dizzy pain, mingles Grecian grandeur, rude wasting of old time, with a billowy main, sun, a shadow of magnitude";
    // Split automatically takes the string myText and passes it into the array, splitting it according to the delimeter. The second arguement in split() is the delimiter. Words between commas are a single entry into the array myArray. 
    String [] myArray = split (myText, ",");  
    
    void setup() {
      size(1000, 1000, P3D); // set size of sketch - the third variable P3D gives it a 3rd dimansion
      smooth();
      font = createFont("Verdana", 1, true); // create the font to be used for text. Although the size used will be random. 
      noFill();
      lastTime = millis();
      img = loadImage("nightsky.jpg");    // load images in preparation for background image
      moon = loadImage("moon.jpg");
      noFill(); // keep spheres as wireframe
    
      // create spheres of varying sizes and adjust detail from default 30 for aestheitc reasons. 
      stroke(gray);
      sphereDetail(maxSphereDetail);
      ball9 = createShape(SPHERE, sphereSize*256);
      sphereDetail(lowSphereDetail);
      ball8 = createShape(SPHERE, sphereSize*128);
      ball7 = createShape(SPHERE, sphereSize*64);
      ball6 = createShape(SPHERE, sphereSize*32);
      ball5 = createShape(SPHERE, sphereSize*16);
      ball4 = createShape(SPHERE, sphereSize*8);
      ball3 = createShape(SPHERE, sphereSize*4);
      ball2 = createShape(SPHERE, sphereSize*2);
      noStroke(); // remove stroke from smallest sphere for best texture effect
      sphereDetail(highSphereDetail); // very smooth small sphere even when zoomed in
      ball1 = createShape(SPHERE, sphereSize);
      ball1.setTexture(moon); // give smallest sphere a texture
    
      minim = new Minim(this); // load sound.mp3 to play as ambient music 
      player = minim.loadFile("sound.mp3", 2048);
      player.play();
    
      cam = new PeasyCam(this, 1000); // set camera's initial position and min max camera position
      cam.setMinimumDistance(camMin);
      cam.setMaximumDistance(camMax);
    }
    
    void draw()
    {
      background(img); // background in draw not setup so drawn on loop and new sphere replaces old - load image variable "nightsky.jpg");
      rotateX (-0.9); // slow roatation slightly after mouse has stopped being used. 
      rotateY(-0.9);
      lights(); // lighting is necessary for the texture to show on the innermost sphere. Also gives a fading in and out effect on the text if the camera is being moved
      // draw spheres
      shape(ball1);  
      shape(ball3);  
      shape(ball4);  
      shape(ball5);  
      shape(ball6);  
      shape(ball7);  
      shape(ball8);  
      shape(ball9);
      translate(width/2, height/2, 300); // set position of spheres (centre of screen)
    
      cam.beginHUD(); // end peasy cam so that words appear in the screen and aren't changed by 'camera' movement and position
      if (millis() < 5000) {
        text("this text will be instructions", width/3, height/3);
        if (millis() - lastTime >= timeToDisplay) // if statement to allow text to fade in and out  
        {
          count = int(random(myArray.length)); // use length of array to get word random within its length (size)
          textSize(random(35, 50)); // random sized text - set min / max values
          randomX = random(100, 500); // random placement of text
          randomY = random(100, 500);
          alphaSetting = 255; // set alpha setting of 255 = fully 'there'
          lastTime = millis();
        }
        fill(white, alphaSetting); // white text
        text(myArray[count], randomX, randomY); //  chose word from myArray - an array of the words from 
        alphaSetting -= a; // fade out of text by decrememtning alpha
    
          cam.endHUD();
      }
    }
    
    void stop() // end sound as can cause an error if continued
    {
      player.close();
      minim.stop();
      super.stop();
    }
    
Sign In or Register to comment.