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

2013/11/7

How to return the center coordinates of an object

JasonZhu JasonZhu

2013/11/7

#
    private boolean pheromoneCenter()
    {
        return pheromones.getWorld().getX();
        return pheromones.getWorld().getY();
    }
I'm having trouble writing this so it compiles. Can someone help me write this?
erdelf erdelf

2013/11/7

#
remove the getWorld() parts
JasonZhu JasonZhu

2013/11/7

#
It says incompatable types even though at the top, I wrote: private Pheromones pheromones;
Zamoht Zamoht

2013/11/7

#
You are trying to make the method return a boolean type, but with getX() you return an int. If you wanted to get the center of the object you would have to create two methods: private int pheromonesCenterX() { //return the x } private int pheromonesCenterY() { //return the y } But is it really shorter to use pheromonesCenterX() instead of just writing pheromones.getX() ?? I do not believe it's worth.
davmac davmac

2013/11/7

#
You declared the method to return a boolean (a true/false value), and then you have two return statements which both return an int (numeric) value. There's multiple problems: 1. The declared return type (boolean) doesn't match the type of the value that you are returning (int) 2. Once a method returns it doesn't keep executing, so it doesn't make sense to have two return statements one after the other. Only the first could actually execute. (In fact, the compiler recognises this as an "unreachable code" error). To fix it you need to figure out what you want the method to actually return, and change the declared return type appropriately. Maybe it would be best to have two methods, since you actually want to return two values. Or, seeing as the method is doing something very simple, perhaps you don't need a separate method at all. Also, this worries me:
It says incompatable types even though at the top, I wrote: private Pheromones pheromones;
You've declared a variable, but did you assign anything to it? You can't get the location of a nothing. You need to identify which pheromone instance you want the coordinates of.
JasonZhu JasonZhu

2013/11/7

#
Ok! I get it now, thanks.
You need to login to post a reply.