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

2021/2/10

Random spawn actors with a button

raul124 raul124

2021/2/10

#
Hi. I'm tring to make a strategy game whit a chess like grid, and i need to spawn 3 new enemys every time a press a button. I like to know how should I do it so they won't overlap each other. If anyone could help me, it would be great. This is my code so far ( just for random spawning) : public void act() { if (Greenfoot.mouseClicked(this)) { 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); getWorld().addObject(new Iniamicii(), 374+j*37, 78+i*19); } } } Greatings, Raul.
danpost danpost

2021/2/10

#
raul124 wrote...
I'm tring to make a strategy game whit a chess like grid, and i need to spawn 3 new enemys every time a press a button. I like to know how should I do it so they won't overlap each other
The following is just an example whose format can be used to accomplish what you want:
import greenfoot.*;

public class Welt extends World
{
    public Welt()
    {
        super(9, 9, 60);
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(this)
            && getObjects(Dot.class).size() < 22)
        {
            for (int k=0; k<3; k++)
            {
                int i = Greenfoot.getRandomNumber(25);
                int v = i/5, u = i%5;
                int x = 4+v-u, y = u+v;
                if (getObjectsAt(x, y, Dot.class).isEmpty()) addObject(new Dot(), x, y);
                else k--;
            }
        }
    }
}
You can do the following to test this code: * create a new scenario; * copy code above to replace MyWorld code; * set default background image of Welt to "cell.jpg"; * create a subclass of Actor, called Dot (image not important); * run and start clicking on background;
raul124 raul124

2021/2/11

#
Thank you very much! The code works now for the not overlapping part. However, I find that the code only work when i click the background, not the actor. Is there any way I could do that inside the background editor? My code so far: public int getRandomNumber(int start,int end) { int normal = Greenfoot.getRandomNumber(end-start+1); return normal+start; } public Joc_in_sine() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(745, 469, 1); } public void act() { Buton Buton_next= new Buton(); addObject(Buton_next, 80, 419); if (Greenfoot.mouseClicked(Buton_next)&& getObjects(Iniamicii.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 (getObjectsAt(374+j*37, 78+i*19, Iniamicii.class).isEmpty()) addObject(new Iniamicii(), 374+j*37, 78+i*19); else k--; } } }
raul124 raul124

2021/2/11

#
Nevermind. I found what i had to do. Thank you very much for you re help.
danpost danpost

2021/2/11

#
raul124 wrote...
Nevermind. I found what i had to do. Thank you very much for you re help.
Out of curiosity, did you test my code?
raul124 raul124

2021/2/17

#
Not the entire code, it wasn t exacty what i needed, but I used some part of it. This part was very helful:
        if (Greenfoot.mouseClicked(this)
            && getObjects(Dot.class).size() < 22)
And this one:
if (getObjectsAt(x, y, Dot.class).isEmpty()) addObject(new Dot(), x, y);
Overall, i were very helpful. Thank you again.
You need to login to post a reply.