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

2022/12/1

How can an actor stop his own act()?

leuveg leuveg

2022/12/1

#
hei, an actor in a game has:
class Auto {
public void act(){
        while ( zielErreicht() == false ) {
            fahre();
        }
    }

    public void fahre(){
        if ( bodenlochVorhanden() == false) {
            move(1);
            Greenfoot.delay(3);
        } else {
            Greenfoot.stop();
        }
    }

    public boolean bodenlochVorhanden() {
        if(getOneIntersectingObject(Bodenloch.class)!=null) {
            return true;
        }
        return false;
    }

    public boolean zielErreicht() {
        if(getOneIntersectingObject(Ziel.class)!=null) {
            return true;
        }
        return false;
    }
}
The actor should fahre() while zielErreicht not true BUT if on the way to the Ziel a Bodenloch the act() of the actor should stop. Problem: the actor dont move but the Programm runs endless. How can i stop it? I tryed Greenfoot.stop() in fahre() but not work. And now?
danpost danpost

2022/12/2

#
leuveg wrote...
I tryed Greenfoot.stop() in fahre() but not work. And now?
The code in the else part starting at line 13 will only execute if BOTH the bodenVorhanden AND the Ziel objects are touching this Auto object. That means that the bodenVorhanden and Ziel objects must overlap (intersect each other). Is that the way you have it?
You need to login to post a reply.