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

2014/5/6

Need Help on Color Sensing (New to Greenfoot)

Speker3 Speker3

2014/5/6

#
I havent done a lot in Greenfoot just some basics of the language, but i am trying to make a tank game and all the classes i make end up to be square, they have transparency but are still part of the class. If i fix this i wont need to go to color sensing but worst case scenario i will be forced to use flat colors for sensing when the bullet reaches the ground.
1
2
3
4
5
6
7
8
public void ColorGravity(int x , int y)
{
    GreenfootImage image = getImage();
    if (image.getColorAt(16, 18) ==  Color.WHITE)
    {
        setLocation(x, y + 2);
    }
}
That is the code i made up for the tanks to see if they are touching ground i have an Import java.awt.Color; for the static colors. Im not sure if its because im trying to sense a color on a transparent part of my tank, or because it will only use colors from the image of my tank. My big question is; Is there a way to use the overall game layer for sensing a color instead of just using the tank image.
danpost danpost

2014/5/6

#
The method you have shown does not make any sense. The 'if' block conditional has two flaws. The first is in your comparison: you should compare colors like this -- 'Color.WHITE.equals(image.getColorAt(16, 18))'. The other is, unless the image of your actor itself changes, the color 'image.getColorAt(16, 18)' will always be the same; which makes the 'if' clause pointless. If this method is in the class of the bullet (which it appears to be), then 'getImage' will get the image of the bullet, which is not, I believe, the image you want to check the color of. To get the image of the background of the world, you can use 'getWorld().getBackground()'; then you would need the x and y off the bullet plus some y offset to determine which point on the background image to check. All images are rectangular, regardless. If you have an excess amount of transparency around your images, you can remove it with my Image Transparency Adder/Trimmer scenario. Download it, open it . Run it, and just load and save the images to remove excess transparency around them.
You need to login to post a reply.