0

I'm making a solar system generator in JavaScript in p5.js and I want to return an array of rgb values from an arrow function but It doesn't work. The star is white instead of yellow, orange or red.

class Star {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.size = random(50, 70);
    this.color = () => {
      let colorChoice = floor(random(6));
      switch(colorChoice) {
        case 0: case 1: case 2: case 3:
          return [255, 255, 0];
          break;
        case 4:
          return [255, 150, 0];
          break;
        case 5:
          return [255, 0, 0];
          break;
      }
    }
  }

  show() {
    noStroke();
    fill(this.color[0], this.color[1], this.color[2]);
    circle(this.x, this.y, this.size);
  }
}

Is there something wrong in the function itself or elsewhere?

1
  • fill(this.color[0], this.color[1], this.color[2]); you are not executing this.color. To do that, you should do this.color(). Also, you should execute it once, not three times. Commented Apr 28, 2020 at 11:47

1 Answer 1

1

this.color - is a function. Try update your show() method to

let color = this.color();
fill(color[0], color[1], color[2]);
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.