Problem with array of objects from a class

Hello,

For Processing 3.5.3 this is what is generated (removed class and other code) and run and can be found in the %temp% folder:

public void setup() {

  for(int i=0;i<nPiece.length;i++){
  
  nPiece[i] = new Snake();
  }
    sSetup(nPiece);
  
}

public void draw() {

  background(0);
  for (int a=0; a<nPiece.length; a++) {
    rect(nPiece[a].x, nPiece[a].y, 10, 10);
  }
}
  public void settings() {  size(1080, 720); }
	
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "sketch_191013b" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

https://processing.org/reference/size_.html

  • In a program that has the setup() function, the size() function must be the first line of code inside setup() , and the setup() function must appear in the code tab with the same name as your sketch folder.

https://processing.org/reference/settings_.html
The settings() method runs before the sketch has been set up, so other Processing functions cannot be used at that point.

https://processing.org/reference/setup_.html

  • If the sketch is a different dimension than the default, the size() function or fullScreen() function must be the first line in setup() .

P5JS reference:
http://processingjs.org/reference/size_/

  • The size() function must be the first line in setup() . If size() is not called, the default size of the window is 100x100 pixels. The system variables width and height are set by the parameters passed to the size() function.

  • Again, the size() method must be the first line of the code (or first item inside setup). Any code that appears before the size() command may run more than once, which can lead to confusing results.

There may be more references.
I posted these for the benefit of the community reading this post.

It does work in Processing 3.5.3 in this example since size() ends up in settings() which is run before the sketch has been set up.

:slight_smile:

2 Likes