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

2017/4/9

How to place actor anywhere except on a wall

vermox vermox

2017/4/9

#
I am currently placing actors in a world which is a house. I want to place them in random locations as long as they are not a wall. Here's some of what I have so far:
        Suspect suspect1 = new Suspect();
        int num1 = getRandomNumber(1, 24);
        int num2 = getRandomNumber(1, 24);
        if(getWorld().getObjectsAt(getX()+dx, getY()+dy, Wall.class).isEmpty()){
            addObject(suspect1, num1, num2);
        }
        Suspect suspect2 = new Suspect();
        num1 = getRandomNumber(1, 24);
        num2 = getRandomNumber(1, 24);
        if(getWorld().getObjectsAt(getX()+dx, getY()+dy, Wall.class).isEmpty()){
            addObject(suspect1, num1, num2);
        }
However I suspect that if the random coordinates are on a wall it will do nothing. How can I modify the code so that the actor will continue to get random coordinates until it is not on a wall, or does this code already make sure of that?
Nosson1459 Nosson1459

2017/4/9

#
You can use a loop:
        do {
            // lines 2 - 6
        } while (suspect1.getWorld() == null);
But I have a problem with your code. What does this actor's collision with a wall have to do with adding a Suspect in some random place in the world. It's not for sure a problem since I don't know how your scenario works but it doesn't seem to make sense to me.
vermox vermox

2017/4/9

#
I suspect that the getWorld, getObjectsAt lines are completely wrong. My game is a murder mystery set in a house with walls dividing up the rooms. I want the suspects to be placed randomly inside the house but not on top of any walls.
danpost danpost

2017/4/9

#
vermox wrote...
I suspect that the getWorld, getObjectsAt lines are completely wrong.
There is nothing wrong with using that for what you are trying to do.
want the suspects to be placed randomly inside the house but not on top of any walls.
You probably do not what even the suspects on top of each other either. I think I would create a method to place object into the world where no object is currently at. That way you can do this:
Suspect suspect1 = new Suspect();
addToRandomEmptyCell(suspect1);
Suspect suspect2 = new Suspect();
addToRandomEmptyCell(suspect2);
Suspect suspect3 = new Suspect();
addToRandomEmptyCell(suspect3);
// etc.
Other Actor sub-types can be added the same way. The method would simply be:
private void addToRandomEmptyCell(Actor actor)
{
    int x = 0, y = 0;
    while (!getObjectsAt(x, y, null).isEmpty())
    {
        x = getRandomNumber(1, 24);
        y = getRandomNumber(1, 24);
    }
    addObject(actor, x, y);
}
Nosson1459 Nosson1459

2017/4/9

#
Nosson1459 wrote...
But I have a problem with your code. What does this actor's collision with a wall have to do with adding a Suspect in some random place in the world. It's not for sure a problem since I don't know how your scenario works but it doesn't seem to make sense to me.
The first code given is in an Actor, since it has getWorld(), getX(), and getY() in it. That code was given by you (danpost) to check if there is a wall in the cell that the actor is about to move to. The code you gave has to be in a World subclass, which makes more sense for adding objects.
danpost danpost

2017/4/9

#
@Nosson1459, yes -- the code I previously gave was for the movement of the actor, which belongs in the class of that actor. This, however, is for the adding of objects into the world -- which should be done in the world class. BTW, the first code-set above also uses 'addObject' without a 'getWorld()' in front of it. With the following:
danpost wrote...
vermox wrote...
I suspect that the getWorld, getObjectsAt lines are completely wrong.
There is nothing wrong with using that for what you are trying to do.
I was referring to the use of 'getObjectsAt' -- sorry for any confusion. (however, the code I provided following that should have clarified that)
Nosson1459 Nosson1459

2017/4/9

#
@danpost Well then there should be an unmentioned compilation error. How can someone use methods from World and Actor in the same class without any prefix. These are the following methods used: Actor:
  • getWorld()
  • getX()
  • getY()
World:
  • addObject(actor, x, y);
danpost wrote...
BTW, the first code-set above also uses 'addObject' without a 'getWorld()' in front of it.
Basically there is a contradiction. As you said: 'addObject(...' but also getWorld, getX, and getY. So vermox's code seems to be in an actor subclass since there are more methods used from there, but he used addObject, and your code seems to be, as it should, in a World subclass.
You need to login to post a reply.