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

2018/10/31

How can I test if all specific actors have been removed from the world?

EMatthee EMatthee

2018/10/31

#
Hi I created a simple game and I need to see if my knight (knight class) has killed all the enemies (enemy class). If so the game goes to the win screen, if not the game must continue. How can I do this?
public void defend()
    {
        Actor Enemy;
        Enemy = getOneObjectAtOffset(0, 0, Enemy.class);
        if (Enemy!=null) 
        {
            World world;
            world = getWorld();
            world.removeObject(Enemy);
        }  
        if (getObjects(Enemy.class).isEmpty)
        {
        Greenfoot.stop();
        getWorld().setBackground(new GreenfootImage("game-won.JPG"));    
        }
}
danpost danpost

2018/10/31

#
EMatthee wrote...
How can I test if all specific actors have been removed from the world? I created a simple game and I need to see if my knight (knight class) has killed all the enemies (enemy class). If so the game goes to the win screen, if not the game must continue. How can I do this?
Move lines 11 through 15 to the act method of your World subclass w/o the 'getWorld(). Also, you need an empty set of parentheses after '.isEmpty'.
EMatthee EMatthee

2018/10/31

#
Thank you, danpost! Another question: Instead of using public void populate (int howMany), how can I ask the player to choose the number of enemies to be randomly placed in the game?
public void populate (int howMany)
    {
        Knight c1 = new Knight();
        addObject(c1, 285, 270);
        sword l1 = new sword();
        addObject(l1, 450, 360);
        for(int i=0; i<howMany; i++) {
            Enemy Enemy = new Enemy();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(Enemy, x, y);
        }
    }
danpost danpost

2018/10/31

#
EMatthee wrote...
T Instead of using public void populate (int howMany), how can I ask the player to choose the number of enemies to be randomly placed in the game? << Code Omitted >>
Remove the method parameter and use the ask method of the Greenfoot class.
EMatthee EMatthee

2018/11/1

#
Please help? I don't know how to do that. Can you please give me the code?
danpost danpost

2018/11/1

#
EMatthee wrote...
Please help? I don't know how to do that. Can you please give me the code?
Example use of ask:
String response = Greenfoot.ask("Enter anything:");
You need to login to post a reply.