size() not working in a different tab?

edited April 2017 in Questions about Code

Hey there. I'm new to processing, so pardon me if it's a dumb question, but I'm kinda dumbfounded.

So, I'm following D. Shiffman's "Nature of Code" course on Youtube.

class Mover {

  PVector position;
  PVector velocity;
  PVector acceleration;
  float mass;

  Mover() {
    position = new PVector(random(width), 0);
    velocity = new PVector(0,0);
    acceleration = new PVector(0,0);
    mass = random(0.5, 4);
  }

  void applyForce(PVector force) {
     PVector f = PVector.div(force, mass);
     acceleration.add(f);
  }

  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    acceleration.mult(0);
  }

  void display() {
    stroke(0);
    fill(127);
    ellipse(position.x,position.y, mass * 20, mass * 20);
  }

  void checkEdges() {

    if (position.x > width) {
      position.x = width;
      velocity.x *= -1;
    } else if (position.x < 0) {
      velocity.x *= -1;
      position.x = 0;
    }

    if (position.y > height) {
      velocity.y *= -1;
      position.y = height;
    }

  }

}

Mover[] movers;


void setup() {
  size(640, 360);
  movers = new Mover[5];
  for(int i = 0; i < movers.length; i++) {
    movers[i] = new Mover();
  }
}

void draw() {
   background(255);

  for(Mover m: movers) {
    PVector gravity = new PVector(0, 0.3);
    gravity.mult(m.mass);
    m.applyForce(gravity);



    if(mousePressed) {
      PVector friction = m.velocity.copy();
      friction.normalize();
      friction.mult(-1);
      float c = 0.1;
      friction.mult(c);
      m.applyForce(friction);
    }


    m.update();
    m.checkEdges();
    m.display();
  }
}

This example works perfectly fine when placed in a single tab. But then I decided to leave the class in one tab and the rest of the stuff in another one. And as soon as my setup() gets moved to a new tab, I get an error "size() cannot be used here" when trying to run the sketch.

Processing considers multiple tabs to be just one big file, doesn't it? So how does this make sense?

Tagged:
Sign In or Register to comment.