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

2012/1/14

getColorAt method

nooby123 nooby123

2012/1/14

#
getWorld().getColorAt(getX(),getY())==new Color(51,153,255) This is the method I'm running. Why won't it work?
danpost danpost

2012/1/14

#
It appears you are trying to compare a point in the background of the world to a specific color. Maybe you mean
getWorld().getBackground().getColorAt(getX(), getY()) == new Color(51, 153, 255)
since 'getColorAt(int, int)' is a method of the GreenfootImage class and needs an image object to execute on.
Builderboy2005 Builderboy2005

2012/1/14

#
That almost works, except for the fact that using the == operator on Objects checks for them being the same object, not to see if they reference the same Data. Instead use the equals() method like so:
getWorld().getBackground().getColorAt(getX(), getY()).equals(new Color(51, 153, 255));
Duta Duta

2012/1/14

#
Builderboy2005 wrote...
That almost works, except for the fact that using the == operator on Objects checks for them being the same object, not to see if they reference the same color. Instead use the equals() method like so:
getWorld().getBackground().getColorAt(getX(), getY()).equals(new Color(51, 153, 255));
This. :P It used to trip me up so much when I first started comparing String's >.<
nooby123 nooby123

2012/1/14

#
Thanks!
danpost danpost

2012/1/14

#
Duta Duta

2012/1/14

#
danpost wrote...
Sorry, I saw the lack of image and went from there, without checking anything else. You know what they say, 'Never assume!'. :< You still got 50% of the fix though, so we forgive you :p
You need to login to post a reply.