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

2014/4/15

Why can't I call the atWorldsEdge method in the world class?

jlee2014 jlee2014

2014/4/15

#
How come I can't do this? What can I do to make it happen? Please help.
danpost danpost

2014/4/15

#
How are you trying to use it? Show what you tried? (some context might help also)
davmac davmac

2014/4/15

#
How come I can't do this?
Because the world itself is never 'at the world's edge' - the world is not in the world; it is the world. If you have a reference to some actor in a variable called 'a' (for example) you can call 'a.atWorldEdge()'.
jlee2014 jlee2014

2014/4/16

#
So using something like this would work? if(Animal.atWorldEdge()&& this != null) { removeObject(Seagull); //world.addObject(newSeagull, worldWidth, Greenfoot.getRandomNumber(worldHeight)); if(Greenfoot.getRandomNumber(9999) < 9) addObject(newSeagull(), 800, Greenfoot.getRandomNumber(560)); } My animal class contains: public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getHeight() - 20) return true; else return false; }
danpost danpost

2014/4/16

#
jlee2014 wrote...
So using something like this would work? < code omitted >
No. 'Animal' is a class, not something that can be at the edge of your world. You need an Animal object; an instance of the Animal class; an actor created from the Animal class or a subclass of Animal.
Animal animal = /** get a reference to an Animal object */;
if (animal.atWorldEdge()) // etc.
You need a specific animal object. But, since you will be calling 'atWorldEdge' from your Seagull and Seagull2 classes, the point here is moot.
You need to login to post a reply.