How can I get a loop in setup() to reset?

I have a for loop in function setup() which I would like to reset and run again when my game resets. The purpose of resetting the loop is to get all the objects in the array back to their start position and colour, or to just create completely new objects.

function setup() {
  createCanvas(550, 550)
  for(let i = 0; i<200; i++) {
    x = random(width)
    y = -50 - 50 * i
    r = random(15,50)
    enemies[i] = new Enemy (x, y, r)
  }
}

I imagine that I won’t be able to do this directly since function setup() can only run once, so is there a workaround to get the setup () for loop to reset?

Link to full code here if needed

>Solution :

Move the relevant code to reset them to another function, perhaps, reset(). Then you can call reset() from setup(), and also call reset() whenever you need to reset them.

Leave a Reply