Defining a class using PVector keeps popping an error message but I really can't figure out.

edited September 2017 in Questions about Code

I tried to define 'Particle' class.

I don't think I have any problems in my code below. but it kept saying'found one too many characters without a to match it"

My code is like below.

class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float lifespan = 255;

  Particle (PVector l) {
    acceleration = new PVector(0,0.05);
    velocity = new PVector(random(-1,1),random(-1,1));
    location = l.get();
  }

  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan = lifespan -2;
  }

  void display() {
    stroke(0,lifespan);
    strokeWeight(2);
    fill(127,lifespan);
    ellipse(location.x, location.y, 12, 12);
  }
}

Answers

  • Maybe your sketch is named Particle as well. If so, rename it to something else! L-)

  • Thank you so much it started to work! But why does the name of tab affect the code?

  • edited September 2017 Answer ✓
    • The name of the 1st tab is also the name used for the wrapper class which encloses everything from all ".pde" tab files.
    • That wrapper class extends Processing's PApplet class.
    • That's the reason why we can freely access the whole Processing's API directly, w/o prefixing them w/ its instance reference.
    • B/c everything becomes members of that wrapper subclass of datatype PApplet.
    • Files w/ extension ".pde" aren't quite valid Java source code.
    • Processing IDE (PDE)'s pre-processor concatenates all ".pde" files and converts them into 1 valid ".java" file.
    • You can check that out by hitting CTRL+E to export the sketch.
    • Then look at the resultant output ".java" file.
    • So, Java doesn't allow nested classes to have the same name as their enclosing classes.
    • And as you know now, all classes we define inside ".pde" files are actually nested classes. ;;)
  • Answer ✓

    Basically, call your sketch something different from your classes.

Sign In or Register to comment.