# I need help with my Space Invaders clone (second round loading improperly)

**URL:** <https://discourse.processing.org/t/i-need-help-with-my-space-invaders-clone-second-round-loading-improperly/46743>\
**Category:** Coding Questions\
**Created:** [July 16, 2025, 7:36pm UTC](https://discourse.processing.org/t/i-need-help-with-my-space-invaders-clone-second-round-loading-improperly/46743 "2025-07-16T19:36:16Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 16, 2025, 11:32pm UTC](https://discourse.processing.org/t/i-need-help-with-my-space-invaders-clone-second-round-loading-improperly/46743/2 "2025-07-16T23:32:40Z")

</div>

Hello @PaddedMellie ,

Please read:  
_[Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147/1)_

It is not necessary to post your entire code.

I suggest that you introduce a state variable to manage state changes:

- Create a global variable called state:  
`boolean state = true; // Initial value`
- Isolate the code that makes the ships and make a function called `make()`  
You can then call this _ **once in setup** _ or _ **as required later.** _
- When there is a state change such as _ **level** _ increasing call the function set `state = true;` which can then be used to call `make()`.
- If you are calling _**make()**_ in draw you only want to do it once and then set  
`state = false;` after so it is not called multiple times for every `draw()` cycle..

Code added to `draw()`:

```auto
boolean state = true;

void draw(){
  background(0, 0, 0);
   
  if (state)
    {
    println(level, state);  
    
    switch(level)
      {
      case 0:
      break;
      
      case 1:
      shipCount = 1;
      make(shipCount);
      break;
      
      case 2:
      shipCount = 3;
      make(shipCount);
      break;
      
      case 3:
      shipCount = 5;
      make(shipCount);
      break;      
      }          
    state = false;

    println(level, state);   
    }    
    
  switch(level)
    {
    // Your original code here
    }
  }

// makes ships
void make(int _noOfShips)
  {
  s = new Spaceships[_noOfShips];

  // Your code   
 }

```

The above worked for me and was from experience.

There are other changes to make and you can work these out.  
Make liberal use of `println()` to see what is (or is not) going on at different points in your code.  
Use the tools available to you to debug… right click on a variable and _Show usage…_ was useful for me to peruse your code.

At some point consider reworking your code.  
The example I provided was _ **patched in…** _ you may be able to integrate this with other code or find a better solution.

> [@PaddedMellie](#):
>
> Should I be recalling the setup function for each level perhaps?

See:

> [@About Restarting](https://discourse.processing.org/t/about-restarting/22557):
>
> Can I write a Reset code for my sketch. Like the Stop and Run method

Have fun!

`:)`

---

_[View the full topic](https://discourse.processing.org/t/i-need-help-with-my-space-invaders-clone-second-round-loading-improperly/46743)._
