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

2019/4/2

Need some help with overlapping actors

renniemm renniemm

2019/4/2

#
I'm fairly new to this, but I've created a game that starts by randomly spawning the actors (20 stars, which the player has to "eat", and 10 monsters which the player has to avoid). The problem is that sometimes the stars and monsters will spawn so close together that it is too difficult for the player to win. I'm thinking I have to use getObjectsInRange but I'm not sure how to use it because it will be a list- and how I will do that for each object. If anyone could at least point me in the right direction, I'd appreciate it! Oh, also, the stars/monsters don't move. They just disappear when eaten. This is where I added them to the game. Not sure if I can just add something inside the for loops maybe- like check each object to see if the list in getObjectsinRange has zero elements and if it doesn't, remove those objects and decrement the for loop that number of times so they will respawn. I'm just not sure how to write out the code for each object that is created because they are all new Star() or new Monster().
for(int i = 0; i<20; i++){
            addObject(new Star(), Greenfoot.getRandomNumber(580)+10,Greenfoot.getRandomNumber(380)+10);
            
        }
for(int j=0; j<10; j++){
            addObject(new Monster(), Greenfoot.getRandomNumber(580)+10, Greenfoot.getRandomNumber(380)+10);
        }
Super_Hippo Super_Hippo

2019/4/3

#
You can choose a new location until a good one is found. The addedToWorld-method is called automatically when an object is added to a world. In there, you can check if it is too near to another object. In the following example, the stars and monsters need to be at least 50 cells away from each other while objects of the same class (star+star, monster+monster) need to be at least 10 cells away from each other. Change those numbers to your needs. If the numbers are too high or the world too small for the numbers, it won't find good locations in the end. Since the stars are added first, you don't really need to check for intersection with monsters for them because no monsters are in the world when they are added. It is just there in case there is a chance you will add new stars at some point which also shouldn't be too near to monsters
//in Star class
protected void addedToWorld(World w)
{
    while (!getObjectsInRange(10, Star.class).isEmpty() || !getObjectsInRange(50, Monster.class).isEmpty())
    {
        setLocation(10+Greenfoot.getRandomNumber(getWorld().getWidth()), 10+Greenfoot.getRandomNumber(getWorld().getHeight()));
    }
}

//in Monster class
protected void addedToWorld(World w)
{
    while (!getObjectsInRange(10, Monster.class).isEmpty() || !getObjectsInRange(50, Star.class).isEmpty())
    {
        setLocation(10+Greenfoot.getRandomNumber(getWorld().getWidth()), 10+Greenfoot.getRandomNumber(getWorld().getHeight()));
    }
}
renniemm renniemm

2019/4/3

#
It works now- thank you!
You need to login to post a reply.