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

2019/5/24

My game wont end when I want it to

AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/24

#
I'm using 3.5.1 and I'm trying to make the game end when all the apples have been collected. The code doesn't bring an error message, but when all the apples are gone, it doesn't end either. Please help- keeping in mind I know barely anything about this language so you might have to talk to me like I'm an idiot.
 {
            if(getObjects(SpaceApple.class).isEmpty())
            {
                Greenfoot.stop();
            }
        }
danpost danpost

2019/5/24

#
AmyIsBadAtCoding wrote...
I'm using 3.5.1 and I'm trying to make the game end when all the apples have been collected. The code doesn't bring an error message, but when all the apples are gone, it doesn't end either. << Code Omitted >>
Where is this code located (method and class)?
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/24

#
its in a purple thingy if that helps, ill just post the entire thing:
import greenfoot.*;

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{

    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Counter counter = new Counter();
        addObject(counter, 66, 29);
        Rocket rocket = new Rocket(counter);
        addObject(rocket, 128, 204);
        SpaceApple spaceapple = new SpaceApple();
        addObject(spaceapple, 154, 57);
        SpaceApple spaceapple2 = new SpaceApple();
        addObject(spaceapple2, 273, 310);
        SpaceApple spaceapple3 = new SpaceApple();
        addObject(spaceapple3, 103, 329);
        SpaceApple spaceapple4 = new SpaceApple();
        addObject(spaceapple4, 358, 104);
        SpaceApple spaceapple5 = new SpaceApple();
        addObject(spaceapple5, 481, 310);
        SpaceApple spaceapple6 = new SpaceApple();
        addObject(spaceapple6, 429, 201);
        SpaceApple spaceapple7 = new SpaceApple();
        addObject(spaceapple7, 521, 64);
        Robot robot = new Robot();
        addObject(robot, 248, 55);
        Robot robot2 = new Robot();
        addObject(robot2, 392, 350);
        Robot robot3 = new Robot();
        addObject(robot3, 43, 372);
        Robot robot4 = new Robot();
        addObject(robot4, 58, 87);
        SpaceApple spaceapple8 = new SpaceApple();
        addObject(spaceapple8, 282, 201);
        SpaceApple spaceapple9 = new SpaceApple();
        addObject(spaceapple9, 516, 157);
        SpaceApple spaceapple10 = new SpaceApple();
        addObject(spaceapple10, 563, 253);
    }
    
        {
            if(getObjects(SpaceApple.class).isEmpty())
            {
                Greenfoot.stop();
            }
        }
    }
danpost danpost

2019/5/24

#
Must be the wrong thingy. Make line 63 to be:
public void act()
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/25

#
Brilliant, it works! Thanks mate
You need to login to post a reply.