Hello everyone, coming back after a long hiatus of not coding and my mind is a bit stumped. I am revisiting an old project so my brain keeps going back to my previous solutions even though I know that wasn’t the best way to do it.
I am trying to create a grid of objects that know their index within the grid both x and y.
This is a simplified version of what I have so far
let tile = []; //Tile Class
let numTiles = 100;
let tileSize = 100;
let gridWidth = 800;
let gridHeight = 600;
let x, y, w, h;
let index;
function setup() {
createCanvas(windowWidth, windowHeight);
for (let i = 0; i < numTiles; i++) {
tile.push(new tileClass(i));
}
}
function draw() {
background(100);
for (let i = 0; i < gridWidth; i += tileSize) {
for (let j = 0; j < gridHeight; j += tileSize) {
tile[0].draw(i, j, tileSize, tileSize);
}
}
}
//CLASS
class tileClass {
constructor(tempIndex) {
this.index = tempIndex;
}
draw(tempX, tempY, tempWidth, tempHeight) {
this.x = tempX;
this.y = tempY;
this.w = tempWidth;
this.h = tempHeight;
this.showTile();
this.showText();
}
showTile() {
noFill();
rect(this.x, this.y, this.w, this.h);
}
showText() {
textAlign(CENTER, CENTER);
fill(255);
textSize(32);
text(this.index, this.x + this.w / 2, this.y + this.h / 2);
}
}
So in setup I know I can give it an initial index, however, later on in the sketch I would like the user to be able to change the number of tiles. With the way I was doing it, the index assign in draw would just count up indefinitely. I don’t know how to draw the objects in an array with a unique index.
I know I am missing something that would make this a ton easier. Like is there a way to create a vector of objects to have both an X and a Y? My previous solution from years ago had things like run = true and solutions like that all over the place so I am trying to find the best solution this time.
Thanks all!