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

2021/6/22

How do I check after all of an actor is gone, then print "GameOver"

anderslieww anderslieww

2021/6/22

#
public class Bat extends Animal { public void act() { move(); setLocation(getX(), getY()+1); if(Greenfoot.isKeyDown("right")) setLocation(getX()+2, getY()); if(Greenfoot.isKeyDown("left")) setLocation(getX()-2, getY()); if(Greenfoot.isKeyDown("down")) setLocation(getX(), getY()+2); if(Greenfoot.isKeyDown("up")) setLocation(getX(), getY()-3); if(canSee(Heart.class)){ eat(Heart.class); Greenfoot.playSound("heal.wav"); } if(canSee(Human.class)){ eat(Human.class); Greenfoot.playSound("suck.wav"); if (getObjects(Human.class).isEmpty()){ World brickWorld = getWorld(); GameOver gameover = new GameOver(); brickWorld.addObject(gameover, 190, 200); Greenfoot.stop(); } } } }
RcCookie RcCookie

2021/6/22

#
You can check whether a given actor is in a world using
boolean isInAWorld = actor.getWorld() != null;
danpost danpost

2021/6/22

#
anderslieww wrote...
How do I check after all of an actor is gone, then print "GameOver"
Use act method in world and check if list of objects in world of that type is empty. Sample code follows (see line 3):
public void act()
{
    if (getObjects(Bat.class).isEmpty())
    {
        setText("GAME OVER", 300, 200);
        Greenfoot.stop();
    }
}
anderslieww anderslieww

2021/6/22

#
Thanks it worked! :)
You need to login to post a reply.