1

I'm using the p5 library for my code, and it seems to break when I try to create a class function using a library function. I went on the reference page, and even the example class functions there use library functions, so I know I'm the one doing something wrong, but I can't seem to figure out what.

Here's my code:

class Enemy {
  constructor(x, y, radius, health) {
    this.x = x
    this.y = y
    this.health = health
    this.r = radius
    this.show();
  }
//  this breaks on strokeWeight();
  show() {
    strokeWeight(4);
    stroke(255, 0, 0);
    noFill();
    ellipse(this.x, this.y, this.r * 2, this.r * 2;
  }
}

This is the error I get:

Uncaught ReferenceError: strokeWeight is not defined (sketch: line 11)

It says that each function is not defined when I try to comment out strokeWeight();, stroke();, noFill();, or ellipse();. If you can help, please do. Thanks.

1 Answer 1

1

I figured it out. I need to put the class after setup() but before the initialization of objects in the class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.