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

2021/4/4

Actor should not spawn on top or close to another actor

hernry2812 hernry2812

2021/4/4

#
I want the Actors of Mole.class to not spawn on top or very close to my main actor Car.class . Therefore I think I need to create another if condition inside the color detection of the generated coordinates. The moles should spawn at least 50 pixels aways from my car. Any help is appreciated^^ To create my moles, I've got the following method:
public void createMoles()
    {
        while (getObjects(Mole.class).size() < 5) 
        {
            int xPos = Greenfoot.getRandomNumber(1920); 
            int yPos = Greenfoot.getRandomNumber(1440);
            int scrolledX = getScrolledX();
            int scrolledY = getScrolledY();
            Color color = getScrollingBackground().getColorAt(xPos, yPos);    
            if (color.getRed() == 77 && color.getGreen() == 77 && color.getBlue() == 77)
            {
                addObject(new Mole(), xPos-scrolledX-560, yPos-scrolledY-360);
            }
        }
    }
danpost danpost

2021/4/4

#
hernry2812 wrote...
I want the Actors of Mole.class to not spawn on top or very close to my main actor Car.class . Therefore I think I need to create another if condition inside the color detection of the generated coordinates. The moles should spawn at least 50 pixels aways from my car.
Just use the following for lines 5 and 6 to not choose coordinates near your main actor:
int xPos = (Greenfoot.getRandomNumber(1820)+50+mainActor.getX())%1920;
int yPos = (Greenfoot.getRandomNumber(1340)+50+mainActor.getY())%1440;
hernry2812 hernry2812

2021/4/4

#
Thanks, it works. But for some reason I am getting errors sometimes when I start/restart the game with my start/restart button. I am using your Scrolling SuperWorld, my window is 800x800 and my background is 1920x1440. This is the error code:
java.lang.IndexOutOfBoundsException: Y is out of bounds. It was: -12 and it should have been at least: 0
	at greenfoot.GreenfootImage.getRGBAt(GreenfootImage.java:601)
	at greenfoot.GreenfootImage.getColorAt(GreenfootImage.java:545)
	at MushroomForest.createMoles(MushroomForest.java:73)
	at MushroomForest.act(MushroomForest.java:60)
	at greenfoot.core.Simulation.actWorld(Simulation.java:573)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
If the error code isn't helping, I could upload a scenario of the game
danpost danpost

2021/4/5

#
Put lines 7 and 8 before lines 5 and 6.. Then try this:
int xPos = (Greenfoot.getRandomNumber(1820)+50+mainActor.getX()+560-scrolledX)%1920;
int yPos = (Greenfoot.getRandomNumber(1340)+50+mainActor.getY()+360-scrolledY)%1440;
hernry2812 hernry2812

2021/4/5

#
Doesn't crash anymore, thanks a lot for your help!
hernry2812 hernry2812

2021/4/7

#
danpost wrote...
Put lines 7 and 8 before lines 5 and 6.. Then try this:
int xPos = (Greenfoot.getRandomNumber(1820)+50+mainActor.getX()+560-scrolledX)%1920;
int yPos = (Greenfoot.getRandomNumber(1340)+50+mainActor.getY()+360-scrolledY)%1440;
Just realized that it doesn't work. I thought it worked but it seems that I had luck that they weren't spawning in my car anymore. I started a game and went afk, a minute after I looked at the health bar and saw that 1 mole spawned in my car. Do you maybe have another method to prevent moles spawning inside my main actor car or 150 pixels next to the car?
danpost danpost

4 days ago

#
This will have no moles spawn along the horizontal or vertical of the main actor.
int scrolledX = getScrolledX();
int scrolledY = getScrolledY();
int xPos = (Greenfoot.getRandomNumber(1720)+100+mainActor.getX()+560+scrolledX)%1920;  //Zufällige Koordinate
int yPos = (Greenfoot.getRandomNumber(1140)+100+mainActor.getY()+320+scrolledY)%1440;
(to include the missing areas along horizontal and vertical not near the actor would be much more complicated)
Super_Hippo Super_Hippo

4 days ago

#
You can try to use this in your Mole class:
protected void addedToWorld(World w)
{
    if (!getObjectsInRange(150, Car.class).isEmpty()) w.removeObject(this);
}
If this is working with the scrolling world...
danpost danpost

4 days ago

#
After some testing, I came up with this:
public void erschaffeMaulwurf()
{
    while (getObjects(Mole.class).size() < 50) //Solange weniger als 2 Maulwürfe da sind
    {
        int scrolledX = getScrolledX();
        int scrolledY = getScrolledY();
        int rand = Greenfoot.getRandomNumber(1920*1440-200*200);
        int xPos = mainActor.getX()+100+560+scrolledX;
        int yPos = mainActor.getY()+100+320+scrolledY;
        xPos = rand < 1920*1240 ? rand%1920 : (xPos+(rand-1920*1240)%1720)%1920;
        yPos = rand < 1920*1240 ? (yPos+rand/1920)%1440 : (yPos-200+(rand-1920*1240)/1720)%1440;
        Color color = getScrollingBackground().getColorAt(xPos, yPos);
        if (color.getRed() == 77 && color.getGreen() == 77 && color.getBlue() == 77)
        {
            Mole mole = new Mole();
            addObject(mole, xPos-scrolledX-560, yPos-scrolledY-320, true);
        }
    }
}
hernry2812 hernry2812

4 days ago

#
Thanks very much danpost! Your help really means a lot to me!
Super_Hippo Super_Hippo

3 days ago

#
Let’s hope this isn’t for a school project and you have to explain your code :)
hernry2812 hernry2812

3 days ago

#
Doch, ist es ;) Aber so wie das aussieht muss ich es anscheinend mit einer Quelle angeben.
You need to login to post a reply.