I'm using an array of objects to randomly print items in a canvas. Each object should have a different random color, but the way I wrote the code every object gets the same one.
var arrayOfObjects = [
{x: 1, y: 5, r: 10, color: pickColor}, //gets random color
{x: 2, y: 6, r: 10, color: pickColor} // should get a different color
//and so on...
];
I tried to replace the pickColor
variable with a function to be executed inside each object so that they all get a different color, but I can't seem to use this value to pick a color inside my color's array.
This obviously doesn't work :
{x: 1, y: 5, r: 10, color: colorList[parseInt(Math.random() * colorList.length)]},
EDIT : this code works, the problem came from an apparently unrelated line with no hints in debugging tool. Thanks for your answers