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

2012/5/31

getColorAt(int x, int y), coordinates relative?

erdelf erdelf

2012/5/31

#
hello, are the coordinates for the method getColorAt relative? and checks it only colors of the image or the other things too?
danpost danpost

2012/5/31

#
You can 'getBackground().getColorAt(x, y)' for the world class image; and you can 'getImage().getColorAt(x, y)' for an actor image (you can even 'GreenfootImage("imageName").getColorAt(x, y)' for an image that is not currently displayed). Either way the x and y are relative to the top-left corner of the image (which is (0, 0)), and you can use the 'getColorAt(x, y)' on any GreenfootImage object.
erdelf erdelf

2012/5/31

#
Is there a way to check the color on specific coordinates, independent from objects on this position, but if there is one, the color of the object should be checked?
danpost danpost

2012/5/31

#
Use the 'getWorld().getObjectsAt(x, y, null)' (if not in the world class) to get a list of the objects that intersect that point. Somehow you will have to figure out which object is on top (it should not be too hard to figure out the class of object on top; that is, if you have used the 'setPaintOrder' command in the world constructor. However, if more that one object of the same class is at that point, the only solution I can think of right now, is to add a Long 'timeAddedToWorld' instance variable to all the actor classes along with a 'addedToWorld(World world) method to set the variable to System time; and use that variable to determine which one will be on top of the others (the one that was added to the world last). From there you would figure out what point in its image is at the location in question and get its color.
erdelf erdelf

2012/6/2

#
hm, maybe. Is there a way I can convert the relative coordinates to coordinates of the world?
danpost danpost

2012/6/2

#
Use getX() and getY(). If 'a' is the actor:
int realLeft = a.getX() - a.getImage().getWidth() / 2;
int realTop = a.getY() - a.getImage().getHeight() / 2;
Remember, if the actor is close to an edge, the values above could end up outside the world bounds.
danpost danpost

2012/6/2

#
To convert world coordinates to the specific coordinates of the image of an actor:
int realX = whatever;
int realY = whatever;
Actor a = getObjectAt(realX, realY, null);
int imageX = realX - a.getX() + a.getImage().getWidth() / 2;
int imageY = realY - a.getY() + a.getImage().getHeight() / 2;
erdelf erdelf

2012/6/2

#
Edit: I saw only your first post, thx
You need to login to post a reply.