# Spawn one sprite when another sprite exits

**URL:** <https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811>\
**Category:** Coding Questions\
**Created:** [October 22, 2020, 8:32am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811 "2020-10-22T08:32:19Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jxjohn](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jxjohn](https://discourse.processing.org/u/jxjohn)\
**Post date:** [October 22, 2020, 8:32am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/1 "2020-10-22T08:32:19Z")

</div>

Hi I’m trying to say that whenever the enemy sprite leaves the screen a mini sprite will be spawned. I already managed to ensure that mini sprites are spawn when enemy leaves the screen but it spawns all eight mini sprite immediately. My thought logic is whenever enemy sprite i plus one but does not increase to the max of 8. However, I tried many ways and still unable to do so. Can anyone help me out thx.

```auto
if (enemyexit == true) {
    for (int i = 0; i<Mcnum; i++) {
      minicreature(mouseX+Mpos[i][0], mouseY+Mpos[i][1]);
      float XDofMDetection = mouseX+Mpos[i][0]-x; // x point difference between 2 of the detection circle
      float YDofMDetection = mouseY+Mpos[i][1]-y; // y point difference between 2 of the detection circle
      float RofMDetection = 40+55; // total radius of both circle
      if (XDofMDetection * XDofMDetection + YDofMDetection * YDofMDetection <= RofMDetection *RofMDetection) { //XDofMDetection * XDofMDetection + YDofMDetection * YDofMDetection code is the distance between centre point of enemny and mini detection circle 
        fill(255, 0, 0);
        textSize(80);
        text("GAMEOVER", 250, 450);
        noLoop();
      }
    }
    Mcnum++;
    }
    if (Mcnum > 8) {
      Mcnum = 8;
  }
}

void MassignV() {
  for (int i = 0; i < 8; i++) {
    Mpos[0][0]= random(120, 150);
    Mpos[0][1]= random(-150, 120);
    Mpos[1][0]= random(-120, -150);
    Mpos[1][1]= random(-150, 120);
    Mpos[2][0]= random(-150, 150);
    Mpos[2][1]= random(120, 150);
    Mpos[3][0]= random(-150, 150);
    Mpos[3][1]= random(-150, -120);
    Mpos[4][0]= random(120, 150);
    Mpos[4][1]= random(-150, 120);
    Mpos[5][0]= random(-120, -150);
    Mpos[5][1]= random(-150, 120);
    Mpos[6][0]= random(-150, 150);
    Mpos[6][1]= random(120, 150);
    Mpos[7][0]= random(-150, 150);
    Mpos[7][1]= random(-150, -120);
  }
}
```

---

<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, 2020, 4:21pm UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/2 "2020-10-22T16:21:11Z")

</div>

I really tried to read your code but I couldn’t find a connection to your question. My bad.

For example can you please write some comments in where we can see which line applies to your question?

---

<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, 2020, 4:52pm UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/3 "2020-10-22T16:52:15Z")

</div>

> [@jxjohn](#):
>
> whenever enemy sprite i plus one but does not increase to the max of 8

I am not sure if this is a sentence

**Idea**

When you want to achieve that minicreatures die and spawn somewhere else,

- either just (ONLY) give the minicreature that just died a new position and movement vector
- OR use an ArrayList where you can kill (remove()) a minicreature from the list and add() a new one. See reference: [Reference / Processing.org](https://www.processing.org/reference/)

Don’t use a for-loop when you only change or add ONE minicreatures

**Remark 1**

```auto
    Mpos[0][0]= random(120, 150);
    Mpos[0][1]= random(-150, 120);

```

please look at PVector in the reference.

Then we would have here: `Mpos[0] = new PVector (random(120, 150), random(-150, 120) );`

Much better.

**Remark 2**

for this line:

```auto
      if (XDofMDetection * XDofMDetection + YDofMDetection * YDofMDetection <= RofMDetection *RofMDetection) { //XDofMDetection * XDofMDetection + YDofMDetection * YDofMDetection code is the distance between centre point of enemny and mini detection circle 

```

look at dist() command in the reference

Thank you.

Warm regards,

Chrisir

😉

---

<div class="post-metadata">

**Author:** ![jxjohn](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jxjohn](https://discourse.processing.org/u/jxjohn)\
**Post date:** [October 22, 2020, 5:53pm UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/4 "2020-10-22T17:53:14Z")

</div>

Hi actually wat I want to achieve is whenever the enemy sprite leaves only one mini sprite appears. If the enemy leaves for the second time, another one appear but the first one still remains. I would like this effect to be achieved the most 8 times. How should I change my code to achieve this effect?

---

<div class="post-metadata">

**Author:** ![jxjohn](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jxjohn](https://discourse.processing.org/u/jxjohn)\
**Post date:** [October 22, 2020, 5:53pm UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/5 "2020-10-22T17:53:48Z")

</div>

Thx for the remark will try to condense my code for better presentation

---

<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, 2020, 8:48pm UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/6 "2020-10-22T20:48:23Z")

</div>

> [@jxjohn](#):
>
> I would like this effect to be achieved the most 8 times.

in my opinion have a spawn-counter and when you spawn, increase it; when it’s \>= 8 don’t spawn

---

<div class="post-metadata">

**Author:** ![jxjohn](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jxjohn](https://discourse.processing.org/u/jxjohn)\
**Post date:** [October 23, 2020, 1:58am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/7 "2020-10-23T01:58:29Z")

</div>

Actually that’s what I’m trying to achieve with the Mcnum. But I don’t really know what’s wrong with it.

---

<div class="post-metadata">

**Author:** ![jxjohn](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jxjohn](https://discourse.processing.org/u/jxjohn)\
**Post date:** [October 23, 2020, 3:18am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/8 "2020-10-23T03:18:02Z")

</div>

Btw do u have any tips on swarming around a larger sprite?

---

<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 23, 2020, 7:41am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/9 "2020-10-23T07:41:37Z")

</div>

> [@jxjohn](#):
>
> that’s what I’m trying to achieve with the Mcnum. But I don’t really know what’s wrong with it.

I guess get rid of the for-loop and make an if-clause as I said

---

<div class="post-metadata">

**Author:** ![jxjohn](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jxjohn](https://discourse.processing.org/u/jxjohn)\
**Post date:** [October 23, 2020, 8:03am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/10 "2020-10-23T08:03:02Z")

</div>

Thx. The way I’m thinking of now is whenever enemy sprite leave the value of i increase by one to affect the array. But this only occurs one if I change my code like this. Where did I go wrong?

```auto
  int i = -1;
  if (enemyexit == true) {
  i++;
  if (i >7){
    i =7;
  } 
      minicreature(mouseX+Mpos[i][0], mouseY+Mpos[i][1]);
}
```

---

<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 23, 2020, 8:10am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/11 "2020-10-23T08:10:34Z")

</div>

I think we said Mcnum has this job, why i now?

Mcnum must be a global variable, so before setup() (you declared i here locally…)

remember we want to count with Mcnum whenever we spawn a mc, so it must be globally

THEN your if-clause: we want to spawn only when Mcnum is \< 8 and increase Mcnum then.

---

<div class="post-metadata">

**Author:** ![jxjohn](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jxjohn](https://discourse.processing.org/u/jxjohn)\
**Post date:** [October 23, 2020, 8:28am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/12 "2020-10-23T08:28:32Z")

</div>

Hi I understood what you are trying to tell me and hv tried to apply it but now whenever enemy sprite leaves the screen it spawns 8 mini sprite in a flickering motion and all the mini sprite immediately disappear. Where did I go wrong?

```auto
if (enemyexit == true) {
  int i = Mcnum;
  if (Mcnum < 8){
      minicreature(mouseX+Mpos[i][0], mouseY+Mpos[i][1]);
}
Mcnum = Mcnum+1;
  }
```

---

<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 23, 2020, 8:58am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/13 "2020-10-23T08:58:29Z")

</div>

```auto

if (enemyexit) {
  if (Mcnum < 8) {
    minicreature(mouseX+Mpos[Mcnum][0], mouseY+Mpos[Mcnum][1]);
    Mcnum = Mcnum+1;
    enemyexit=false;
  }
}

```

---

<div class="post-metadata">

**Author:** ![jxjohn](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@jxjohn](https://discourse.processing.org/u/jxjohn)\
**Post date:** [October 23, 2020, 9:18am UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/14 "2020-10-23T09:18:46Z")

</div>

thx chris  
this helped me a lot

---

<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 23, 2020, 12:19pm UTC](https://discourse.processing.org/t/spawn-one-sprite-when-another-sprite-exits/24811/15 "2020-10-23T12:19:25Z")

</div>

> [@jxjohn](#):
>
> Btw do u have any tips on swarming around a larger sprite?

See website Examples | flocking for a swarm

It would be that the swarm is attracted to but also repelled by your object

Search the Forum or web, lots of examples
