title: Java Game Leveling Up
body: <p>I'm trying to figure out a way to navigate from one level to another 3 times. I have 3 game levels and I'm trying to level up to go from level 1 to level 2, and from level 2 to level 3. Right now it goes from level 1 to level 3 and then from level 3 to level 3 again. I know I'm going wrong somewhere, can anyone help:</p>
  
  <p>My 3 levels extends my <code>GameLevel</code> class, and my main class is <code>Game</code>, where <code>goToNextLevel</code> method is created (which stops the current level and levels up while populating it).</p>
  
  <pre><code>package game;
  public class Game {
  
      /** The World in which the bodies move and interact. */
      private GameLevel world;
  
      /** A graphical display of the world (a specialized JPanel). */
      private UserView view;
  
      private int level;
  
      private Controller controller;
  
      private SoundClip gameMusic;
  
      /** Initialize a new Game. */
      public Game() {
          // make the world
          level = 1;
          world = new Level1();
          world.populate(this);
          try {
              gameMusic = new SoundClip("data/backgroundaudio.wav");   // Open an audio input stream
              gameMusic.loop();  // Set it to continous playback (looping)
          } catch (UnsupportedAudioFileException|IOException|LineUnavailableException e) {
              System.out.println(e);
          } 
          // make a view
          view = new MyView(world, world.getPlayer(), 1250, 700);
          // uncomment this to draw a 1-metre grid over the view
          // view.setGridResolution(1);
          // display the view in a frame
          JFrame frame = new JFrame("Collect All The Snowballs And Pick Up The Keys!");
          // quit the application when the game window is closed
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocationByPlatform(true);
          // display the world in the window
          frame.add(view);
          // don't let the game window be resized
          ColourPanel colourPanel = new ColourPanel(view);
          frame.add(colourPanel, BorderLayout.WEST);
          frame.setResizable(false);
          // size the game window to fit the world view
          frame.pack();
          // make the window visible
          frame.setVisible(true);
          // get keyboard focus
          frame.requestFocus();
          // give keyboard focus to the frame whenever the mouse enters the view
          view.addMouseListener(new GiveFocus(frame));
          controller = new Controller(world.getPlayer());
          frame.addKeyListener(controller);
          // start!
          world.start();
      }
  
      /** The player in the current level. */
      public SnowCollector getPlayer() {
          return world.getPlayer();
      }
  
      /** Is the current level of the game finished? */
      public boolean isCurrentLevelCompleted() {
          return world.isCompleted();
  
      }
  
      /** Advance to the next level of the game. */
      public void goNextLevel() {
          world.stop();
          if (level == 3) {
              System.exit(0);
          } else  {
              level++;
              // get a new world
              world = new Level2();
              // fill it with bodies
              world.populate(this);
              // switch the keyboard control to the new player
              controller.setBody(world.getPlayer());
              // show the new world in the view
              view.setWorld(world);
              world.start();
          }
      }
  
      /** Run the game. */
      public static void main(String[] args) {
          new Game();
      }
  }
  </code></pre>
  
  <p>Below is my <code>GameLevel</code> class that the class of each of my 3 levels extends</p>
  
  <pre><code>/**
   * A level of the game.
   */
  public abstract class GameLevel extends World {
      private SnowCollector player;
  
      public SnowCollector getPlayer() {
          return player;
      }
  
      /**
       * Populate the world of this level.
       * Child classes should this method with additional bodies.
       */
      public void populate(Game game) {
          player = new SnowCollector(this);
          player.setPosition(startPosition());
          Door door = new Door(this);
          door.setPosition(doorPosition());
          door.addCollisionListener(new DoorListener(game));
      }
  
      /** The initial position of the player. */
      public abstract Vec2 startPosition();
  
      /** The position of the exit door. */
      public abstract Vec2 doorPosition();
  
      /** Is this level complete? */
      public abstract boolean isCompleted();
  }
  </code></pre>
  
  <p>This code is where the <code>goToNextLevel</code> method is called when my main character makes contact with a door</p>
  
  <pre><code>package game;
  
  /**
   * Listener for collision with a door.  When the player collides with a door,
   * if the current level is complete the game is advanced to the next level. 
   */
  public class DoorListener implements CollisionListener {
      private Game game;
  
      public DoorListener(Game game) {
          this.game = game;
      }
  
      @Override
      public void collide(CollisionEvent e) {
          SnowCollector player = game.getPlayer();
          if (e.getOtherBody() == player &amp;&amp; game.isCurrentLevelCompleted()) {
              System.out.println("Going to next level...");
              **game.goNextLevel();**
          }
      }
  }
  </code></pre>
  
  <p>I know the problem is with the <code>game.goToNextLevel()</code> method at the top.</p>