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

2013/4/17

Color Detection

1
2
Alright, this is from my Slender game. You will have to modify this where the Angel constantly checks if the Player can see it, or have the Player send the message to Angel:
public boolean seeSlender()
    {
        //To make sure it's on the screen (I have a scrolling game, if you do, keep this, and change 275 to the value you want, if not, remove this)
        if (getObjectsInRange(275, Slender.class).size() == 0)
            return false;
        Slender s = (Slender)getObjectsInRange(275, Slender.class).get(0);  //Or angel, if that's what you want, again, you can get rid of this and replace it with Slender s = (Slender)getWorld().getObjects(Slender).get(0) if you have multiple angels, read the code below this method
        int sX = s.getX();  //x Location of the slender/angel
        int sY = s.getY();  //y location
        int dir = getRotation(); //current direction
        turnTowards(sX, sY); //Turns it towards slender/angel
        int dir2 = getRotation();  //new directiong
        setRotation(dir);  //set direction back to normal (so it looks like nothing happend)
        return dir - dir2 <= /*The beam's range / 2 */ && dir - dir2 >= /*The beam's range / -2*/;
    }
Here's the code for multiple angels:
for (Slender /*or angel*/ s : getWorld().getObjects(Slender.class)
{
        int sX = s.getX();
        int sY = s.getY();
        int dir = getRotation();
        turnTowards(sX, sY);
        int dir2 = getRotation();
        setRotation(dir);
        if (dir - dir2 <= /*Beam's range / 2*/ && dir - dir2 >= /*Beam's range / -2*/
        return true;
}
return false;
I'm going to have dinner, so I won't be able to help for the next 10 - 20 minutes. Hoped that helped
danpost danpost

2013/4/25

#
What happens when you replace line 3 in the angel class with the following lines and run the scenario:
if (img == null)
{
    System.out.println("the image is not present");
    Greenfoot.stop();
    return;
}
Color imgColor = img.getColorAt(getX(), getY());
if (color.equals(imgColor))
JetLennit JetLennit

2013/4/25

#
Sorry, i have already done @FlyingRabidUnicornPig's... and it worked!
You need to login to post a reply.
1
2