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

2024/12/13

Check all aliens are dead with getObject?

BBB BBB

2024/12/13

#
I have an alien actor type and would like to use get object to see if all the aliens have been removed so I can switch to the win screen of my game. for some reason this code:
if(getWorld().getObjects(Alien.class) == null)
        {
            WinScreen w = new WinScreen();
            Greenfoot.setWorld(w);
            getWorld().removeObject(this);
        }
isn't working. what am I doing wrong?
danpost danpost

2024/12/13

#
BBB wrote...
I have an alien actor type and would like to use get object to see if all the aliens have been removed so I can switch to the win screen of my game. for some reason this code: << Code Omitted >> isn't working. what am I doing wrong?
You are using the wrong condition on line 1. The returned value using getObjects is NEVER null. It might be empty, however. I would put this code in your world class, myself. Checking for game over seems like an action that is the responsibility of the world, In world class:
public void act() {
    if (getObjects(Alien.class).isEmpty()) {
        Greenfoot.setWorld(new WinScreen());
    }
}
You need to login to post a reply.