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

2012/4/28

How to detect an object in an Area?

lgbrf lgbrf

2012/4/28

#
I need to detect if one object is in one of three areas, and then i have a method to remove it - removePlayer(); how can i accomplish this?
lgbrf lgbrf

2012/4/28

#
i guess my question is more specifically, how do i write a certain area - so say 50-100,50-100
danpost danpost

2012/4/28

#
If you are just looking for Player class objects in those areas, it would be easier to iterate through a list of all Player class objects and compare their locations to those of the areas.
lgbrf lgbrf

2012/4/28

#
how would i do that?
danpost danpost

2012/4/28

#
It would be something like this:
for (Actor player : getWorld().getObjects(Player.class))
{
    if ((player.getX() >= 50 && player.getX() <= 100 && player.getY() >= 50 && player.getY() <= 100)  removePlayer();
    // same for other areas
}
lgbrf lgbrf

2012/4/28

#
awesome thanks
lgbrf lgbrf

2012/4/29

#
ok, i modified and used this and got- private boolean inRiver(){ return (Player.getX() >= 65 && Player.getX() <= 85|| Player.getX() >= 115 && Player.getX() <= 135|| Player.getX() >= 165 && Player.getX() <= 185|| Player.getX() >= 215 && Player.getX() <= 235); } non-static method getX() cannot be referenced from a static context
danpost danpost

2012/4/29

#
That is because you are using the class name instead of a reference to an object of that class. Add
Player player = (Player) getWorld().getObjects(Player.class).get(0);
Then change all your 'Player.getX()'s to 'player.getX()'. If there is any chance that the Player class object is not in the world, you would have to check for an empty List before getting the first item from it. PLEASE NOTE: If this code is actually in your Player class, DO NOT add or change anything, but DO remove all occurances of 'Player.' from the code.
You need to login to post a reply.