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

2020/6/27

How to advance in levels in a game?

APCS2020 APCS2020

2020/6/27

#
We have 4 levels (DemoWorld, Maze1, Maze2, Maze3) and we want to advance a level every time cheeseCount == 2 (change to Maze1), cheeseCount == 52 ( go to Maze2) and cheeseCount ==102 (go to Maze3) The program changes to Maze1 after all the cheese is collected in DemoWorld but after we enter aMaze1 and collect all the cheese, the world doesn't change.....
    
public class Mouse extends Actor{
    private int cheeseCount=0;  
        Actor cat = getOneIntersectingObject(Cat.class);
        if(cat!=null){
            World DemoWorld = getWorld();
            Greenfoot.setWorld(new Maze1());
            
        }
	
        Actor falcon = getOneIntersectingObject(Falcon.class);
        if(falcon!=null){
            World DemoWorld = getWorld();
            Greenfoot.setWorld(new Maze1());
            
        }
        
        Actor snake = getOneIntersectingObject(Snake.class);
        if(snake!=null){
            World DemoWorld = getWorld();
            Greenfoot.setWorld(new Maze1());
            
        }

        Actor c  = getOneIntersectingObject(Cheese.class);
        if(c != null){
            cheeseCount++;
            getWorld().removeObject(c);
        }
        
        if(getWorld().getObjects(Cheese.class).size() == 0) {
           changeLevel();
        }
        
        
    }    
    public void changeLevel() {
        if(cheeseCount == 102) {
           Greenfoot.setWorld(new Maze3());
        }
        if(cheeseCount == 52){ 
           Greenfoot.setWorld(new Maze2());
        }
        if(cheeseCount == 2) {
           Greenfoot.setWorld(new Maze1());
        }
    }
}

danpost danpost

2020/6/27

#
APCS2020 wrote...
We have 4 levels (DemoWorld, Maze1, Maze2, Maze3) and we want to advance a level every time cheeseCount == 2 (change to Maze1), cheeseCount == 52 ( go to Maze2) and cheeseCount ==102 (go to Maze3) The program changes to Maze1 after all the cheese is collected in DemoWorld but after we enter aMaze1 and collect all the cheese, the world doesn't change..... << Code Omitted >>
Add a second condition to lines 37, 40 and 43 that requires the specific type of world (DemoWorld, Maze1 and Maze2).
You need to login to post a reply.