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

2017/5/23

how to move to next level

ali18 ali18

2017/5/23

#
How do I move to the next level after I have reached a certain score. Level one requires the player to get a score of 20 in order to move on to level two of my game. This is what i have got at the moment and it's not working. This is the code for my counter
public class Counter extends Actor
{
   private int totalCount = 0;
   int Counter = 0;
   public Counter ()
   {
       setImage(new GreenfootImage("0" , 20, Color.WHITE, Color.BLUE));
   }
   public void bumpCount (int amount)
   {
       totalCount += amount;
       setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLUE));
   }
    private void nextLevel()
   {
       if (Counter >= 20) 
       {
           if (getWorld() instanceof level1) Greenfoot.setWorld(new level2());
           else if (getWorld() instanceof level2) Greenfoot.setWorld(new level3());
           else if (getWorld() instanceof level3) Greenfoot.setWorld(new Win());
        }
   }
}
Super_Hippo Super_Hippo

2017/5/23

#
Why do you have two different int fields? Remove line 4 and change 'Counter' in line 16 to 'totalCount'. Then, you also have to add this line at the end of the 'bumpCount' method to call the 'nextLevel' method.
nextLevel();
ali18 ali18

2017/5/24

#
I have removed line 4 and changed line 16 counter to totalCount. However, I did not understand what you meant by the second sentence. Can you explain in further detail please. thank you. The changes have shown below:
public class Counter extends Actor
{
   private int totalCount = 0; // Start point of the counter
   public Counter ()
   {
       setImage(new GreenfootImage("0" , 20, Color.WHITE, Color.BLUE));
   }
   public void bumpCount (int amount)
   {
       totalCount += amount;
       setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLUE));
   }
   if (totalCounter >= 20) 
   {
       if (getWorld() instanceof level1) Greenfoot.setWorld(new level2());
       else if (getWorld() instanceof level2) Greenfoot.setWorld(new level3());
       else if (getWorld() instanceof level3) Greenfoot.setWorld(new Win());
   }
}
danpost danpost

2017/5/24

#
ali18 wrote...
I did not understand what you meant by the second sentence. Can you explain in further detail please
Insert the one-liner given at line 13 in your initial code posting. Hippo did not want you to mess with the 'nextLevel' method itself -- just add a call to that method as the last thing the 'bumpCount' method does.
ali18 ali18

2017/5/25

#
Hello so the code that you have gave works very good. However, how am i able to change the score for the other levels. So, i want to collect more than 20 to move onto level 3 in level 2. Therefore, now in level 1, i move onto level 2 after collecting 20 but now i want to catch 35 in level 2 to move onto level 3.
Super_Hippo Super_Hippo

2017/5/25

#
You could change the order of the conditions:
private void nextLevel()
{
    if (getWorld() instanceof level1)
    {
        if (totalCounter >= 20) Greenfoot.setWorld(new level2());
    }
    else if (getWorld() instanceof level2)
    {
        if (totalCounter >= 35) Greenfoot.setWorld(new level3());
    }
    else if (getWorld() instanceof level3)
    {
        //...
    }
}
ali18 ali18

2017/5/25

#
Thank you very much. This is working very well now.
You need to login to post a reply.