Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


#Actualwarnexus

Posted 09 February 2013 - 08:10 PM

The idea was making the gameLoop false to pause the game and unpause the game by making the gameLoop true again. It was able to pause and unpause once before the only way to close the game was to use Eclipse's terminate button instead of the x button in the top corner of the game window. 
 
public class Game extends Canvas implements KeyListener{
 
      private boolean isRunning;   
      
      public static void main(String[] args){
        getInstance().run();
      }   
      
      // Singleton pattern get instance
    public static Game getInstance(){
        if (instance == null){
            instance = new Game();
            
        }
        return instance;
        
    }  
 
      private Game(){  
      isRunning = true;  
      }
 
      public void run(){
         // basic game loop
         while(isRunning){
         // draws objects here
         }
      }
 
          @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        switch (e.getKeyCode()) 
        {
        
        // pause the game 
        case (KeyEvent.VK_P):
            
        getInstance().setIsRunning(false);
                    
        break;
        // unpause the game
        case (KeyEvent.VK_U):
 
        getInstance().setIsRunning(true);
        run();
 
        break;
        }
    }
 
}

#1warnexus

Posted 09 February 2013 - 06:32 PM

The idea was making the gameLoop false to pause the game and unpause the game by making the gameLoop true again. It was able to pause and unpause once before the only way to close the game was to use Eclipse's terminate button instead of the x button in the top corner of the game window. 
 
public class Game extends Canvas implements KeyListener{
 
      private boolean isRunning;   
      
      public static void main(String[] args){
        getInstance().run();
      }   
      
      // Singleton pattern get instance
    public static Game getInstance(){
        if (instance == null){
            instance = new Game();
            
        }
        return instance;
        
    }  
 
      private Game(){  
      isRunning = true;  
      }
 
      public void run(){
         // basic game loop
         while(isRunning){
         // draws objects here
      }
 
          @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        switch (e.getKeyCode()) 
        {
        
        // pause the game 
        case (KeyEvent.VK_P):
            
        getInstance().setIsRunning(false);
                    
        break;
        // unpause the game
        case (KeyEvent.VK_U):
 
        getInstance().setIsRunning(true);
        run();
 
        break;
        }
    }
 
}

PARTNERS