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

2013/4/17

Color Detection

1
2
JetLennit JetLennit

2013/4/17

#
How do i have an actor detect if it is on a certain color (in the background, in other actors, etc.) and also (In a different way) detect if a color is near them
danpost danpost

2013/4/17

#
You are going down a long road that is not very well lit. What I mean is that the more points you check, the more precise you are in looking for specific colors, CPU time will be robbed and lag will occur. You can get the GreenfootImage object for the world backround with 'getBackground' and you can get the images of actors with 'getImage' and then you can use the GreenfootImage method 'getColorAt' to retrieve the color at any point in the image. Then you can use the Color methods 'getRed', 'getGreen', 'getBlue' and 'getAlpha' to get the color portions of that color. Finally, compare all portions with the values you are looking for.
JetLennit JetLennit

2013/4/17

#
Okay thank you, if it does slow it down too much i can just create an image and overlap where the things are
Gevater_Tod4711 Gevater_Tod4711

2013/4/17

#
To get the color of an image you can use the method getColorAt(int x, int y). This method returns the color at a specific point of a GreenfootImage. You just have to get the reference to the image of which you want to detect the color (E.g. the getBackground() method of the world returns the background image). You can find an example here.
JetLennit JetLennit

2013/4/17

#
So... to see if a certain color where i am i should do
if(getColor(getX(), getY() == /*what do i put here?*/)
{
    //the stuff i want to do
}
Gevater_Tod4711 Gevater_Tod4711

2013/4/17

#
No this will not work. Try to do it like this:
GreenfootImage img = ...;//the image you want to check;
Color color = ...;//the color you want to check;
if (img.getColorAt(x, y).equals(color)) {
   //at the position you searched you have found the color you wanted;
}
JetLennit JetLennit

2013/4/17

#
Thank you @Gevater_Tod4711 and @danpost! I will probably not make this game for a bit... but i will do it eventually
JetLennit JetLennit

2013/4/24

#
Is img a string?
MatheMagician MatheMagician

2013/4/25

#
No, img is a greenfootImage. If you look at the beginning of Gevater_Tod4711's code, there is a new variable declared:
GreenfootImage img = ...;//the image you want to check; 
Now, you probably want to reference the background of the actor, so you would do something like:
GreenfootImage img = getImage();
MatheMagician MatheMagician

2013/4/25

#
If this is for the world class, it would be something like:
GreenfootImage img = getBackground();
JetLennit JetLennit

2013/4/25

#
I knew that there was a variable... i just didn't know what to put in the variable I get a null pointer exeption, i have this in the angel class
GreenfootImage img = Beam.img;  //the image you want to check;
        Color color = Color.green;//the color you want to check;
        if (img.getColorAt(getX(), getY()).equals(color)) 
        {
            canMove = false;
        }
        else
        {
            canMove = true;
        }
and this in the Beam class
//i have this variable declared
public static GreenfootImage img; 
//then in act i have this
img = getImage();  
Are you doing something where the actor reacts when the color of the beam is over it? Because I found out another way to do that, without using colors.
JetLennit JetLennit

2013/4/25

#
Yes! (or at least in this case, this isn't going to be the only game i need this for)
danpost danpost

2013/4/25

#
If you are getting that error on line 3 in the angel class, try changing it to:
if (color.equals(img.getColorAt(getX(), getY())))
JetLennit JetLennit

2013/4/25

#
still gives a null pointer exception.... and i forgot to say that the null pointer exception is on line three of the angel class
There are more replies on the next page.
1
2