This site requires JavaScript, please enable it in your browser!
Greenfoot back
raul124
raul124 wrote ...

2021/2/17

Spawn new actors when there are no bullets

raul124 raul124

2021/2/17

#
Hello guys! When i press a buton on my screen, i want all the enemys to shot bullets, then, when the bullets are gone, spawn new enemy. But I can t figure out how to spawn them after the bullets are gone and not before. Here is my code so far (the coordonates are for a grid that i made for the map, don t mind them):
if (Greenfoot.mouseClicked(this))
        {         
            for(int i=1;i<9;i+=2)
                for(int j=-3;j<=3;j+=2)
                    if(!getWorld().getObjectsAt(376+j*37, 61+i*19, Inamicii.class).isEmpty())
      
                            getWorld().addObject(new proiectil(), 376+j*37, 85+i*19);
            for(int i=0;i<=8;i+=2)
                for(int j=-4;j<=4;j+=2)
                    if(!getWorld().getObjectsAt(376+j*37, 61+i*19, Inamicii.class).isEmpty())
                       
                            getWorld().addObject(new proiectil(), 376+j*37, 85+i*19);
        }
       if (Greenfoot.mouseClicked(this)&& getWorld().getObjects(Inamicii.class).size() < 24)
        { 
            for(int k=1;k<=3;k++)
            {
                int i=Greenfoot.getRandomNumber(9);
                int j=0;
                if(i%2==0)
                {
                    if(i==4)    j=getRandomNumber(-2,2);
                    else if(i==2||i==6)  j=getRandomNumber(-1,1);
                    else    j=0;
                    j=j*2;
                }
                else if(i==1||i==7)  
                    while(j==0)         
                        j=getRandomNumber(-1,1);
                else
                    while(j%2==0)         
                        j=getRandomNumber(-3,3);
                if (getWorld().getObjectsAt(376+j*37, 60+i*19, Inamicii.class).isEmpty())    
                {
                if(getWorld().getObjectsAt(376+j*37, 60+(i+1)*19, Inamicii.class).isEmpty())   
                {
                   getWorld().addObject(new Inamicii(), 376+j*37, 60+i*19);
                   getWorld().addObject(new umbra(), 376+j*37, 80+i*19);
                }
                else
                    getWorld().addObject(new Inamicii(), 374+j*37, 60+i*19);
           
                }
                else k--;
            }
        }
    }
If someone could help me with this problem, I would be very grateful.
danpost danpost

2021/2/17

#
raul124 wrote...
When i press a buton on my screen, i want all the enemys to shot bullets, then, when the bullets are gone, spawn new enemy. But I can t figure out how to spawn them after the bullets are gone and not before
If you remove the button (or make it inactive) when clicked, you can add it back in (or reactivate it) AND add new enemies when no bullets are in the world.
raul124 raul124

2021/2/22

#
Your idea worked. But after some thinking I ended up makind to different buttons, one for each action I needed. Thank you very much!
You need to login to post a reply.