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

2018/5/28

How to check if actor is touching a certain color

Zweeg Zweeg

2018/5/28

#
I need to check if my actor is touching the RGB value (88,40,16). However I've already tried the lines of code: GreenfootImage img = getWorld().getBackground(); Color mars = new Color(88, 40, 16); if(img.getColorAt(getX(),getY()).equals(mars) && yVel>5) { } when I use this code nothing happens and it acts like it is not touching the color. (By the way the color belongs to an actor but I can't use the ifTouching method because the actor has a transparent background and the transparent background takes up the whole screen and then the code would recognize it as touching the actor and it would trigger early.
danpost danpost

2018/5/28

#
Zweeg wrote...
By the way the color belongs to an actor
Then why would you bother with the image of the background of the world (in code given)? You would need the image assigned to the actor, determine where on it you need to check and get the color from there. There may be an easier way to do what you want, but it is impossible to determine if this is so, and how, with the limited information given.
Zweeg Zweeg

2018/5/28

#
@danpost so how would I get the image of another actor? The only get methods I know that could be possibly useful are the getBackground() method and the getImage() method but when I use the getImage method it gives me a terminal error that x or y is out of bounds.
danpost danpost

2018/5/28

#
Zweeg wrote...
@danpost so how would I get the image of another actor? The only get methods I know that could be possibly useful are the getBackground() method and the getImage() method but when I use the getImage method it gives me a terminal error that x or y is out of bounds.
Get the actor first. Then get its image. You need the actor not only for the image but also for its location in the world so you can determine where the particular pixel of its image is located. If the actor covers the entire world, then use 'getOneIntersectingObject'.
Zweeg Zweeg

2018/5/28

#
@danpost could you provide some example code? I'm not quite sure how to use getOneIntersectingObject or how to use it to become useful in this case
danpost danpost

2018/5/28

#
Zweeg wrote...
@danpost could you provide some example code? I'm not quite sure how to use getOneIntersectingObject or how to use it to become useful in this case
Would rather see if a better way is available. But:
1
2
3
Actor mars = getOneIntersectingObject(Mars.class);
GreenfootImage marsImg = mars.getImage();
// etc.
should be how to get going.
You need to login to post a reply.