0

I am trying to move a rectangle up,down,left,right when up,down,left,right keys are pressed. I am puzzled why the key listeners no longer listen when the game is started..i.e when gameloop is started. Before starting the game or pressing "start" the listener works i.e it prints "up pressed" System.out.println("up pressed"); but when the game is started by clicking the start button then the listener no longer work. Why is this so? Appreciate any help!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GameLoopTest extends JFrame implements ActionListener
{
   private GamePanel gamePanel = new GamePanel();
   private JButton startButton = new JButton("Start");
   private JButton quitButton = new JButton("Quit");
   private JButton pauseButton = new JButton("Pause");
   private boolean running = false;
   private boolean paused = false;
   private int fps = 60;
   private int frameCount = 0;

   public GameLoopTest()
   {
      super("Fixed Timestep Game Loop Test");
      Container cp = getContentPane();
      cp.setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(1,2));
      p.add(startButton);
      p.add(pauseButton);
      p.add(quitButton);
      cp.add(gamePanel, BorderLayout.CENTER);
      cp.add(p, BorderLayout.SOUTH);
      setSize(1000, 700);

      startButton.addActionListener(this);
      quitButton.addActionListener(this);
      pauseButton.addActionListener(this);



      Action handle_up_action_pressed = new AbstractAction(){

          public void actionPerformed(ActionEvent e){

              gamePanel.up_pressed= true;

              System.out.println("up pressed");


          }

      };


      Action handle_up_action_released = new AbstractAction(){

          public void actionPerformed(ActionEvent e){

            gamePanel.up_pressed= false;

          }

      };


      Action handle_down_action_pressed = new AbstractAction(){

          public void actionPerformed(ActionEvent e){


            gamePanel.down_pressed= true;

          }

      };


      Action handle_down_action_released = new AbstractAction(){

          public void actionPerformed(ActionEvent e){


            gamePanel.down_pressed= false;

          }

      };


      Action handle_left_action_pressed = new AbstractAction(){

          public void actionPerformed(ActionEvent e){


            gamePanel.left_pressed= true;

          }

      };


      Action handle_left_action_released = new AbstractAction(){

          public void actionPerformed(ActionEvent e){


            gamePanel.left_pressed= false;



          }

      };


      Action handle_right_action_pressed = new AbstractAction(){

          public void actionPerformed(ActionEvent e){


            gamePanel.right_pressed= true;


          }

      };


      Action handle_right_action_released = new AbstractAction(){

          public void actionPerformed(ActionEvent e){

            gamePanel.right_pressed= false;



          }

      };




      gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "handle_up_pressed");
      gamePanel.getActionMap().put("handle_up_pressed", handle_up_action_pressed);

      gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "handle_up_released");
      gamePanel.getActionMap().put("handle_up_released", handle_up_action_released);

      gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "handle_down_pressed");
      gamePanel.getActionMap().put("handle_down_pressed", handle_down_action_pressed);

      gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "handle_down_released");
      gamePanel.getActionMap().put("handle_down_released", handle_down_action_released);

      gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "handle_left_pressed");
      gamePanel.getActionMap().put("handle_left_pressed", handle_left_action_pressed);

      gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), "handle_left_released");
      gamePanel.getActionMap().put("handle_left_released", handle_left_action_released);

      gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "handle_right_pressed");
      gamePanel.getActionMap().put("handle_right_pressed", handle_right_action_pressed);

      gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "handle_right_released");
      gamePanel.getActionMap().put("handle_right_released", handle_right_action_released);














   }

   public static void main(String[] args)
   {
      GameLoopTest glt = new GameLoopTest();
      glt.setVisible(true);
   }

   public void actionPerformed(ActionEvent e)
   {
      Object s = e.getSource();
      if (s == startButton)
      {
         running = !running;
         if (running)
         {
            startButton.setText("Stop");
            runGameLoop();
         }
         else
         {
            startButton.setText("Start");
         }
      }
      else if (s == pauseButton)
      {
        paused = !paused;
         if (paused)
         {
            pauseButton.setText("Unpause");
         }
         else
         {
            pauseButton.setText("Pause");
         }
      }
      else if (s == quitButton)
      {
         System.exit(0);
      }
   }

   //Starts a new thread and runs the game loop in it.
   public void runGameLoop()
   {
      Thread loop = new Thread()
      {
         public void run()
         {
            gameLoop();
         }
      };
      loop.start();
   }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3059427
  • 209
  • 2
  • 3
  • 10
  • You're running the game on a listener, so no more events are handled until the game stops running. – Darkhogg Dec 05 '13 at 21:10
  • [for example](http://stackoverflow.com/questions/7940173/how-do-i-use-keyeventdispatcher/7940227#7940227), and to set focus to JPanel e.g. InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); – mKorbel Dec 05 '13 at 21:15
  • Thanks mKorbel! JComponent.WHEN_IN_FOCUSED_WINDOW fixed the problem.But, why? what was the default focus setting? – user3059427 Dec 05 '13 at 21:23
  • @Darkhogg what do you mean by running the game on a listener? can you clarify? – user3059427 Dec 05 '13 at 21:23

0 Answers0