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

2013/7/24

What's wrong with it?

Kartoffelbrot Kartoffelbrot

2013/7/24

#
I want to check wether an actor is between both ends of another actor with caring about their graphical extend. It doesn't work, but I don't know why.
public boolean onActor(Actor p){
    if(p!=null){
        int links = p.getX()-p.getImage().getWidth()/2;
        int rechts= p.getX()+p.getImage().getWidth()/2;
        if(links<=getX()-getImage().getWidth()/2)
            return true;
        if(rechts>=getX()+getImage().getWidth()/2)
            return true;
    }
    return false;
    }
danpost danpost

2013/7/24

#
I think you need to check for the opposite and return false if not found on; then if both checks fail, return true. The way you have it now, it will check one side, and if true, return true and not check the other side.
danpost danpost

2013/7/24

#
It should be like this:
public boolean onActor(Actor p){
    if(p==null) return false;
    int links = p.getX()-p.getImage().getWidth()/2;
    int rechts = p.getX()+p.getImage().getWidth()/2;
    if (links >getX()-getImage().getWidth()/2) return false;
    if (rechts < getX()+getImage().getWidth()/2) return false;
    return true;
}
Kartoffelbrot Kartoffelbrot

2013/7/25

#
Or I put line 5 and line 7 together to one if-statement. Thank you, I understood now whats wrong with it. It works now.
You need to login to post a reply.