Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading