Regarding adding an element with listener to the array:
jets[i].OnJetOutOfBorder( () => {
const stars = animatedBatch.getInstancesOf(RandomStar);
if( stars.length < maxNumOfStars ){ //leverContrast.getValue() > 0.01 &&
//threshhold
if(1 - Math.random() < 0.9){
console.log('_____________________________star listener occured________')
addStar(i);
}
}
});
and modifying it to exclude expired:
// Draw and filter only "alive" shapes
this.animatedItems = this.animatedItems.filter((item) => {
item.draw(this.delta_ms);
return item.isAlive; // leave only shapes with isAlive=true
});
You have the same issue as in the question: [https://stackoverflow.com/questions/79117802/array-push-is-losing-elements-added-by-the-class-listener/79118760#79118760][1]:
filter code breaks things because it replaces the array that addEventListeners.
Modify it accordingly:
// EDIT Modify the existing array, must work from end to beginning
for( let next = this.animatedItems .length - 1; next >= 0; --next ) {
// First draw it
this.animatedItems [next].draw( this.delta_ms )
// Then remove it if it's finished it's itterations
if( !this.animatedItems [next].isAlive) this.animatedItems .splice(next, 1)
}