# Enemy spawning

**URL:** <https://forum.makecode.com/t/enemy-spawning/16778>\
**Category:** Help\
**Created:** [December 7, 2022, 6:01pm UTC](https://forum.makecode.com/t/enemy-spawning/16778 "2022-12-07T18:01:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BruceSchwa](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@BruceSchwa](https://forum.makecode.com/u/BruceSchwa)\
**Post date:** [December 7, 2022, 6:01pm UTC](https://forum.makecode.com/t/enemy-spawning/16778/1 "2022-12-07T18:01:58Z")

</div>

I want to make my space shooter game harder. I want it to start off having enemy’s thrown at you every 500 ms. At score 10 I want to make it so the enemy’s shoot at you every 100 ms. I appreciate all the help you people give.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [December 7, 2022, 6:41pm UTC](https://forum.makecode.com/t/enemy-spawning/16778/2 "2022-12-07T18:41:45Z")

</div>

Great question!

So right now I’m guessing you have code that looks something like this:

 ![Screenshot 2022-12-07 at 10.32.41 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/69af90fd67eb5c6dbed60659e18cb2ecc2e72203.png)

The **on game update ever x ms** event always runs at the same rate, so there is no way to make the time interval shorter with that block. Instead, I recommend you use the **forever** block.

The **forever** block is similar to **on game update** with one crucial difference: you can use the **pause** block inside it. If I were to rewrite the above code with **forever** instead of **on game update** , it would look like this:

 ![Screenshot 2022-12-07 at 10.36.30 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/52b626772a68483971ced491384f2c21af15ba27.png)

Because **pause** takes in a number, we can pass in different numbers to pause for different amounts of time. So to change the speed once the score reaches 10 you could use an if-statement like this:

 ![Screenshot 2022-12-07 at 10.38.17 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/3/3907edee6b2bb0b2e6e3293503a8c7eee252417c.png)

Or if you wanted to smoothly transition between the two numbers, you could use some math like this:

 ![Screenshot 2022-12-07 at 10.40.24 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/4/4a7d561ddd1632dfaec088ccf1987a70fee65edc.png)

---

<div class="post-metadata">

**Author:** ![BruceSchwa](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@BruceSchwa](https://forum.makecode.com/u/BruceSchwa)\
**Post date:** [December 13, 2022, 5:20pm UTC](https://forum.makecode.com/t/enemy-spawning/16778/3 "2022-12-13T17:20:03Z")

</div>

![Screen Shot 2022-12-13 at 11.11.58 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/5d3e2fe4fea9ebf2c43fd3b365637c5e508458bf.jpeg)

Thanks so much. Unfortunately, the student was out ill for some time. He just forwarded what he has thus far. Thank you for any assistance you can lend!

---

<div class="post-metadata">

**Author:** ![BruceSchwa](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@BruceSchwa](https://forum.makecode.com/u/BruceSchwa)\
**Post date:** [December 20, 2022, 5:53pm UTC](https://forum.makecode.com/t/enemy-spawning/16778/4 "2022-12-20T17:53:25Z")

</div>

@richard just wondering if you can help me with my reply to your suggestions. Thanks

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [December 20, 2022, 6:21pm UTC](https://forum.makecode.com/t/enemy-spawning/16778/5 "2022-12-20T18:21:54Z")

</div>

@BruceSchwa sure thing!

Looking at this code, i think my above suggestion should work for speeding up enemy spawns. I do see another issue here though; they’re using the “enemy1” variable inside of their overlap events instead of the local variables.

When writing an overlap event (or any other event), you almost always want to use the local variables that you drag out of the event like this:

![drag-out-local](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/1/135e70f18dd285f7fc5f9fe81b9b0fce451541f3.gif)

The reason why is that **all variables created in the variables category are global**. Every time they create a new enemy inside of that **on game update** event, the global “enemy1” variable is being updated to point to whatever the most recently created enemy is. Destroying it will destroy the most recently created enemy instead of the enemy that is actually overlapping with the player/projectile.

The variables you drag out of events are local, they refer to whatever sprites are actually involved in the event.
