I'm currently working on an evolution simulation app that involves reproducing organisms with a health level above 75%. After reproduction, their health is reduced by half. However, I'm encountering an issue where my app crashes in p5.js for reasons unknown to me.
To troubleshoot this problem, I have attempted to reduce the number of organisms to just 3 and encapsulate the process within a function of the organism's class.
var organisms = []; // array to store organism instances
function reproduce(){
for (let i = 0; i < organisms.length; i++){
if(organisms[i].life > 0.75){
// create genetically similar size
let size = organisms[i].size + (random() > 0.5 ? 1 : -1 * random() * 2);
// declare instance
let org = new Organism(width, height, size)
organisms.push(org);
// prevent infinite reproduction
organisms[i].life -= 0.5;
}
}
}
My expectation was for this code to simply generate new instances of the class, but unfortunately, p5.js still crashes.