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

2013/6/12

Changing levels

rachel1495 rachel1495

2013/6/12

#
How do I use a counter to change levels. I want to add 1 to the counter at the end of each level. When the counter = 1, level 2 should come up then when counter = 2, level 3 should come up. This is my code in the player class:
    if(world1.levelCount.getValue() == 1)
          
{
                BalloonWorld world = (BalloonWorld)getWorld();  
                getWorld().removeObjects(getWorld().getObjects(Balloon1.class));
                world.Level2();  
            }
  
       
        }
      public void count()
        {
            if(getWorld().getObjects(Balloon2.class).isEmpty())
              {
                  ((BalloonWorld) getWorld()).countLevels();
                }
            }
This is my code in the world: public void countLevels() { levelCount.add(1); }
davmac davmac

2013/6/12

#
I think you're missing a '{' after the 'if' statement in the first line. (Maybe you lost it when copying the code). Other than that, the code looks basically ok. You just need to add more levels by duplicating the entire 'if' block, and check for levelCount.getValue() == 2, and so on. Or, is something going wrong with what you have currently?
rachel1495 rachel1495

2013/6/12

#
This code works to set level 2 but when it sets level 2, it keeps repeating itself and setting level 2 so I do not know why
davmac davmac

2013/6/13

#
Ah. I guess that the "if" block is in some code that gets called by an act() method, which gets called every cycle. levelCount.getValue() still returns 1, so the condition is true and level 2 will be set continuously. One way to avoid this is to only set the level when the levelCount is increased. For instance, move the "if" block (from the player class) into the world's countLevels() method.
You need to login to post a reply.