# Spawn one sprite when another sprite exits

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.

``````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);
}
}``````

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

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: https://www.processing.org/reference/

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

Remark 1

``````    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:

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

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?

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

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

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

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

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

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?

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

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.

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?

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

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

thx chris
this helped me a lot

1 Like

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