i cannot be resolved to a variable

edited November 2017 in How To...

Is there something that's wrong? I've put newGame; into setup so i'd think it should have initalized i. Thank you.

float[] x = new float[20];
float[] y = new float[20];
void setup() {
  background(200);
  size(600, 600);
  fill(255);
  rect(0, 0, width, height - 120);
  newGame();
}

void draw() {
    fill(1);
  textSize(25);
  text("Last Key:" + key , 40, 520);
  ellipse(x[i], y[i], 30, 30);
}

void newGame() {
  for (int i = 0; i < x.length; i++) {
    x[i] = int(random(width));
    y[i] = int(random(height-130));
  }
}

Answers

  • edited November 2017 Answer ✓

    This error means, to the program, that the variable doesn't exist. The reason for this is that a variable can only "travel" to parts in the program below it. This is kind of a weird concept so I'll try to explain it pictorally : Here

    In a sense, if a variable is created in a "box" (like the draw method) it can only travel down into boxes inside of the box it was created in. Meaning, in the picture example, if I made a variable in the draw loop, it could be used inside the if statement and the for loop, but NOT the setup "box" since it's a seperate box that isn't inside the draw box.

    So in your program, you create the variable i inside the newGame method/"box", and as a result, it can't travel outside of the newGame "box". So it might seem like the solution would be to move the creation of the i variable to the top of the program with your arrays, but this actually wouldn't work. The for loop executes all at once before any other part of the program executes, meaning i will always be x.length - 1 when the draw loop is being executed.

    Instead, you can make an additional for loop for the ellipse that is the same size as your other for loop, so that it goes through every single one :

    All you need to modify thus is changing the ellipse line so that it uses this for loop, such that the overall program looks like :

    float[] x = new float[20];
    float[] y = new float[20];
    void setup() {
      background(200);
      size(600, 600);
      fill(255);
      rect(0, 0, width, height - 120);
      newGame();
    }
    
    void draw() {
        fill(1);
      textSize(25);
      text("Last Key:" + key , 40, 520);
      for (int i = 0; i < x.length; i++) {
       ellipse(x[i], y[i], 30, 30);
      }
    }
    
    void newGame() {
      for (int i = 0; i < x.length; i++) {
        x[i] = int(random(width));
        y[i] = int(random(height-130));
      }
    }
    

    Hope this helps! Let me know if you have any more questions

  • Short answer: variable scope. Your variable i in your function newGame() only exist inside the for loop. As soon as the for loop is done, the variable is trashed.

    @Dinoswarleafs suggestion would work. My question to you is what do you want to do with your arrays? Do you want to generate the ellipses all at once in each frame? Do you want to generate a new set of arrays in every frame? Or maybe generate a new set of arrays (both x and y) every 30 frames? Check for instance:

    final int NEW_GAME=30;
    int ctr=0;
    
    float[] x = new float[20];
    float[] y = new float[20];
    void setup() {
      background(200);
      size(600, 600);
      fill(255);
      rect(0, 0, width, height - 120);
      //fill(1);
      textSize(25);
      key='?';
    }
    
    void draw() {
      background(200);
      ctr++;
      if(ctr==NEW_GAME){
        newGame();
        ctr=0;
      }
      text("Last Key:" + key , 40, 520);
      for (int i = 0; i < x.length; i++) {
       ellipse(x[i], y[i], 30, 30);
      }
    }
    
    void newGame() {
      for (int i = 0; i < x.length; i++) {
        x[i] = int(random(width));
        y[i] = int(random(height-130));
      }
    }
    

    Kf

  • This helped a lot, thanks!

Sign In or Register to comment.