FINNNNN
FINNNNN

Reputation: 157

Issues when calling object function: "Script error. (: line 0)" in p5.js online editor

I don't think there is anything wrong with the syntax in this simple code. But as long as I call generator.display(); in the draw function, an error message pops up: "Script error. (: line 0)".

You can see and run the code here:https://editor.p5js.org/huskyspeaks/sketches/-dN7ZQ9pg

As you might find out (assuming there is nothing wrong with the online editor), removing generator.display(); will remove the error. But I really don't understand why this is the case. I don't see anything wrong with the way this simple frame is coded.

var generator;

function setup() {
  createCanvas(400, 640);
  generator = new Generator(width / 2, height / 2, 4);
}


function draw() {
  background(55);
  generator.display();
}

var Generator = function(x, y, m) {
  this.pos = createVector(x, y);
  this.mass = m;

  this.display = function() {
    ellipse(pos.x, pos.y, 10 * mass, 10 * mass);
  }
}

If indeed there is something wrong with the code, how could I update it ?

Upvotes: 0

Views: 1971

Answers (1)

maazadeeb
maazadeeb

Reputation: 6112

You're missing references to this.

ellipse(this.pos.x, this.pos.y, 10 * this.mass, 10 * this.mass);

You create pos and mass on this but reference it without it. Changing it to as shown above fixes it.

Upvotes: 2

Related Questions