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

2020/3/23

Color error

HelpMehCode HelpMehCode

2020/3/23

#
Hello. I am having trouble with coding colours. Here is my code. Does anyone know how to fix this error. The error is "incompatible types: Greenfoot.Color cannot be converted to java.lang.Classes<?>"
public boolean hitWalls()
    {
        if (isTouching(Color.BLACK))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
danpost danpost

2020/3/23

#
What objects have the images of the walls?
RcCookie RcCookie

2020/3/23

#
The method isTouching only works with a second actor. What you could do is this, but it only checks for the exact location the actor is on:
public boolean hitWalls(){
    return getWorld().getColorAt(getX(), getY())==Color.BLACK;
}
Alternatively you could also just make the wall an Actor. Then you could use ifTouching.
RcCookie RcCookie

2020/3/24

#
By the way you can shorten your code al lot like this:
//instead of this
if(isTouching(Color.BLACK)){
    return true;
}
else{
    return false;
}

//this:
return isTouching(Color.Black);
RcCookie RcCookie

2020/3/24

#
Hello again, I made a small mistake; take this Code:
return getWorld().getColorAt(getX(), getY()).equals(Color.BLACK);
You need to login to post a reply.