fab680
fab680

Reputation: 39

Returns 'Undefined'

How come during the "draw" each Point returns "undefined" instead of a coordinate? Is it not possible to fill an array during setup?

let a = [];
var scl = 10;

class Point {
  constructor(i, j) {
    this.x = i;
    this.y = j;
  }
}

function setup() {
  createCanvas(600, 600);
  for(x = 0; x < width; x += scl) {
    a[x] = [];
    for(y = 0; y < height; y += scl) {
      let p = new Point(x, y);
      a[x][y] = p;
    }
  }
}

function draw() {
  background(0);
  
  a.forEach( p => {
    rect(p.x, p.y, scl);
  });
}

Upvotes: 0

Views: 73

Answers (1)

Barmar
Barmar

Reputation: 780842

a is a 2-dimensional array, you need nested loops:

function draw() {
    background(0);
    a.forEach(row => row.forEach(p => rect(p.x, p.y, scl)));
}

Upvotes: 1

Related Questions