# Spawning Enemies

**URL:** <https://discourse.processing.org/t/spawning-enemies/14875>\
**Category:** Coding Questions\
**Created:** [October 22, 2019, 5:24pm UTC](https://discourse.processing.org/t/spawning-enemies/14875 "2019-10-22T17:24:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![OrangeJuice](https://avatars.discourse-cdn.com/v4/letter/o/5e9695/32.png) [@OrangeJuice](https://discourse.processing.org/u/OrangeJuice)\
**Post date:** [October 22, 2019, 5:24pm UTC](https://discourse.processing.org/t/spawning-enemies/14875/1 "2019-10-22T17:24:35Z")

</div>

I’m trying to create a side scrolling shoot 'em up type game and I’m having trouble spawning the enemies, this is my code right now.

```
if (r==1 && enemy==true)

  buggieY+=5;
buggieX=750;
buggieY=300;

if (buggieY>600 || buggieY<0)
{
  buggieY*=-1;
} 
enemyY=buggieY;
enemyX-=25;
if (enemyX<0)
{
  enemyX=750;
}
if (x>725 && x<775 && y>buggieY-50 && y<buggieY+50)
{
  enemy=false;
  enemykilled+=1;
  buggieY=-100;
  buggieX=-100;
} else
{
  buggieY=-100;
  buggieX=-100;
}

```

I’m newer to processing so any help would be nice,  
Thank you

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [October 22, 2019, 6:47pm UTC](https://discourse.processing.org/t/spawning-enemies/14875/2 "2019-10-22T18:47:34Z")

</div>

You need functions triggered by some event like mousePressed() and of course the special Processing void draw() function. This function will be called by an internal timer and will run at a frame rate of about 60 times per second, looping your code. Without that it will run your code just once .

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2019, 7:05pm UTC](https://discourse.processing.org/t/spawning-enemies/14875/3 "2019-10-22T19:05:51Z")

</div>

I guess you have setup and draw and you posted only the relevant part of your code.

When you want to have multiple enemies shown at the same type, make an array or arraylist of them.

When only one enemy is visible at a time, you can respawn it by placing it to the very right. I think you do this with `enemyX=750;` already.

You can also use a random here: `enemyX=int(random(width+22, width + 130));`

So don’t say `enemy=false;` and instead just place the enemy on the far right.

Welcome to the forum!

Chrisir

---

<div class="post-metadata">

**Author:** ![OrangeJuice](https://avatars.discourse-cdn.com/v4/letter/o/5e9695/32.png) [@OrangeJuice](https://discourse.processing.org/u/OrangeJuice)\
**Post date:** [October 23, 2019, 3:33pm UTC](https://discourse.processing.org/t/spawning-enemies/14875/4 "2019-10-23T15:33:35Z")

</div>

Thanks for the help and for the welcome, hope you gave a great day!
