Text wont draw in setup()

okay so i have this code and im trying to draw text() in setup() right before i load and play a sound file. the problem is, the text does not draw until after the sound has loaded. if the sound code is commented out, the text loads fine.

void setup(){ 
  size(1100,800);
  background(20,20,20);
  noStroke();

  //Tri-force
  fill(255,255,0);
  triangle(420, 400, 532, 180, 644, 400);
  triangle(308, 620, 420, 400, 532, 620);
  triangle(532, 620, 644, 400, 756, 620);

  //Loading
  fill(255,255,255,trans-80);
  circle(1050,750,20);
  fill(255,255,255,trans-60);
  circle(1000,750,20);
  fill(255,255,255,trans-40);
  circle(950,750,20);
  text("This is a loading screen!", 50, 20); // this is the text

  file = new SoundFile(this,"PrincessZelda.mp3");
  file.play();

  //Volume meter(0-1)
  file.amp(.5);

}

im not sure what im doing wrong or why the text refuses to draw

this is probably in the wrong category but i didn’t know where else to put it. sorry

Yeah, the screen is not updated throughout but just at the end of the function setup ().

You can move this into draw() :

file = new SoundFile(this,“PrincessZelda.mp3”);
file.play();

//Volume meter(0-1)
file.amp(.5);

And surround it with an if clause so it runs only once:

if(firstTime) {

…
firstTime=false;
}

Before setup you say

boolean firstTime=true;

3 Likes

Im pretty sure I tried this but the sound didn’t end up playing, but i’ll try again and I’ll update you!

Also just to note, everything but the text draws so that’s why im confused

1 Like

Hello @starzorrow ,

Here is an example that loads the sound file in the background and sets boolean flags for decisions on program flow/control:

//https://www.youtube.com/watch?v=P-KjGrbuov4

import processing.sound.*;

SoundFile file;

boolean loaded = false;
boolean done = false;

void setup()
  { 
  size(300, 300);
  textSize(48); 
  textAlign(CENTER, CENTER);
  thread("loadFile");
  }
  
void draw()
  {
  background(255, 0, 0);
  
  if(loaded == true)
    {
    file.play();
  
    //Volume meter(0-1)
    file.amp(1);
    loaded = false;
    done = true;
    } 

  if(done == false) 
    {
    fill(255);
    textSize(48);  
    text(frameCount, width/2, height/2-10);
    return; // jumps to end of draw() function
    }
    
    // Resume draw() cycle...
  
    background(0, 255, 0); 
    fill(0);
    text(frameCount, width/2, height/2-10);
    }
  
  void loadFile() 
    {
    file = new SoundFile(this, "Loituma - Ievas polka Ievan Polkka good sound.mp3");
    loaded = true;
    }

Please looks up all references to code here:

The draw() reference states:

All Processing programs update the screen at the end of draw(), never earlier.

This also applies to setup() as well which is frameCount = 0;

When testing your code I had to add a fill(255) for the text otherwise I did not see the text on the black background.

:)

2 Likes

Okay so I tried it, the text now shows up, but the sound never loads. the program suspends because of the thread()

That was the issue in the initial code

	fill(255);
	textSize(30);
	text("This is the loading screen!", 20, 20);



	//music
	file = new SoundFile(this,"PrincessZelda.mp3");
	file.play();

	//Volume meter(0-1)
	file.amp(.5);

with the fill(255), the text appears to still not show while everything else does)