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

2017/4/24

Levels don't change

boilboil boilboil

2017/4/24

#
So I have some code here, it essentially codes for when one level is finished, it progresses to the next level.
public void act() {
        String key = Greenfoot.getKey();
        if (state == MENU) {
             if ( key == "space" ) {
                    state = PLAYING;
             }
        }else if (state == PLAYING) {
             if (level == 0) {
                 level ++;
                 startGame();
                 drawLevel(1);
             }
        } else if ( level == 1 && (getObjects( Block.class ) == null)) {
                level ++;
                resetGame();
                startGame();
                drawLevel (2);
        }
    }
    
    private void startGame () {
        Putin BouncingPutin = new Putin();
        addObject ( BouncingPutin, 600, 410 );        
        PADDLE Paddle = new PADDLE();
        addObject ( Paddle, 600 , 610  ); 
        removeObjects(getObjects(Title.class));
    }
    
    private void resetGame () {
        removeObjects(getObjects(PADDLE.class));
        removeObjects(getObjects(Putin.class));
        removeObjects(getObjects(Block.class));
    }
    
    private void drawLevel ( int level ) {
        if ( level == 1 ) {
            makeBlock ( 100, 250, 30, 10 );
        }
        if ( level == 2 ) {
            makeBlock ( 100, 150, 30, 10 );
        }
    }
But the problem is that when the level is finished ( getObjects( Block.class ) == null ) the next level does not load. So why does this happen, how do I fix it, and how can I prevent this from happening ever again?
danpost danpost

2017/4/24

#
boilboil wrote...
the problem is that when the level is finished ( getObjects( Block.class ) == null ) the next level does not load.
And it will never load with that code. To never have this happen again, you must remember that 'getObjects' ALWAYS returns a List object and the existence of an object means it cannot be null ('null' means no object is referenced or given). That List object returned may be empty (or have a size of zero), which is what you should be checking: ( getObjects(Block.class).size() == 0 ) or ( getObjects(Block.class).isEmpty() )
boilboil boilboil

2017/4/25

#
Alright, thanks. But the problem still sticks, I don't know why. HELP! SOS
private final int MENU = 0; 
    private final int PLAYING = 1;
    private int state = MENU;
    private int level = 0;
    
    
    
    public bounciner()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 625, 1, false); 
        Title title = new Title();
        addObject ( title, 550 , 325  ) ;
        
    }
    
    public void act() {
        String key = Greenfoot.getKey();
        if (state == MENU) {
             if ( key == "space" ) {
                    state = PLAYING;
             }
        }else if (state == PLAYING) {
             if (level == 0) {
                 level ++;
                 startGame();
                 drawLevel(1);
             }
        } else if ( level == 1 && (getObjects(Block.class).isEmpty())) {
                level ++;
                resetGame();
                startGame();
                drawLevel (2);
        }
    }
    
    private void makeBlock ( int startX, int startY, int length, int height ) {
        int posX = startX;
        int posY = startY;
        for ( int i = 0; i < height; i++ ) {
            makeRow ( posX, posY, length );
            Block tBlock = new Block();
            posY -= tBlock.getImage().getHeight();
        }
        
        
    }
    
    
    private void makeRow ( int startX, int startY, int length ) {
        int posX = startX;
        int posY = startY;
        for ( int i = 0; i < length ; i++ ) {
            Block block = new Block();
            addObject ( block, posX, posY );
            posX += block.getImage().getWidth();
        }
    }
    
    private void startGame () {
        Putin BouncingPutin = new Putin();
        addObject ( BouncingPutin, 600, 410 );        
        PADDLE Paddle = new PADDLE();
        addObject ( Paddle, 600 , 610  ); 
        removeObjects(getObjects(Title.class));
    }
    
    private void resetGame () {
        removeObjects(getObjects(PADDLE.class));
        removeObjects(getObjects(Putin.class));
        removeObjects(getObjects(Block.class));
    }
    
    private void drawLevel ( int level ) {
        if ( level == 1 ) {
            makeBlock ( 100, 250, 30, 10 );
        }
        if ( level == 2 ) {
            makeBlock ( 100, 150, 30, 10 );
        }
    }
    
}
If you really want to look at my entire code, here mates
danpost danpost

2017/4/25

#
Are you sure that no Block objects remain in the world? To test (when you think that none remain), pause the scenario, right click on the world background and select the 'getObjects(Class)' method. Key in 'Block.class' for the parameter and hit the OK button to see what comes up. Inspect the returned List object and check its size. Report back the result.
boilboil boilboil

2017/4/25

#
Yeah, when I do that, it says that there is nothing in the list returned, like the length of the list is 0
danpost danpost

2017/4/26

#
boilboil wrote...
Yeah, when I do that, it says that there is nothing in the list returned, like the length of the list is 0
Okay. Try removing the 'else' from line 29.
boilboil boilboil

2017/5/15

#
AIGHT! Thanks so much. I haven't been checking up on this, but thanks for your help Dan.
You need to login to post a reply.