Fullscreen width and height variables returning 100

Hi! I have seen width and height returning 100 or incorrect values when a sketch is run in fullScreen() many times. Is there a way to get the accurate dimensions of the screen?

I solved it. I’m a dumb ;D. I put a class that i got wrong width and height in outside setup and initialized it outside setup (before setup is called) so I got default values of 100

void setup(){
  fullScreen();
}

Game mygame = new Game(); // <----- A class I got the wrong values in the constructor
1 Like

I am having the same problem, and I don’t really understand what you’re saying. I use “width” after I define the size:

void setup() {
  fullScreen();
  background(255);
}

float y=width;

void draw() {
  text(y,109,109);
//After here is just the exit button
  fill(255);
  rect(20, 20, 50, 30);
  fill(0);
  text("Exit", 30, 40);
}
void mousePressed() {
  if (mouseX >= 20 && mouseX < 70&&mouseY >= 20 && mouseY < 50) {
      exit();
    }
}
}

P.S. The extra code is just a “exit” button

I am not really sure but I think the code outside of functions is executed first (Correct me if I’m wrong) then the setup then draw functions. Try putting “y = width;” at the end of setup() like here:

float y;

void setup() {
  fullScreen();
  background(255);

  y = width;
}

Notice the declaration of y outside of setup so It is a global variable (accessible outside of setup in this case)

exactly ! A variable that is declared outside a method is visible to that method, but not the other way round. All variables that are “outside” are done first, so calling on width will return its default value and size(x,y) will change these variables later.

@Aoyanco: “First” does not mean the order of lines in that case, but the hierarchy of things. You can write any variable anywhere in your code, if it is not inside a method (including) setup, it will be done before setup happens.

4 Likes

Variable scope:

Coding Train 6.4: Variable Scope - Processing Tutorial

:)

1 Like