Results 1 to 5 of 5
  1. #1
    ultimate is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Java 2D game - creating menu

    I am trying to create Menu to my 2D game in Java (Sokoban). But the game is not responding to pressed keyes. Menu is working, but when I choose new game in menu (by pressing Enter), game starts but it does not react to pressed keyes. But, when I remove menu, game is responding to pressed keys ... but when there is also menu, it is not responding. How to solve this problem ?

    Here is Sokoban class:
    Java Code:
    public final class Sokoban extends JFrame  {
        
        private static Menu menu;
        private static Board board;
        private static JFrame myFrame;
        private static Action enterAction;
            
        public Sokoban() {
            InitUI();
        }
    
        public void InitUI() {
        	
        	myFrame = new JFrame();
        	   	
        	myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myFrame.setSize(800,600);
            myFrame.setLocationRelativeTo(null);
            myFrame.setTitle("Labyrint");
            myFrame.setResizable(false);
            
            board = new Board();
            menu = new Menu(); 
                   
            myFrame.add(menu);
            
            enterAction = new EnterAction();
            
            menu.getInputMap().put( KeyStroke.getKeyStroke( "ENTER" ), "doEnterAction" );
            menu.getActionMap().put( "doEnterAction", enterAction );
            
            myFrame.setVisible(true);
       
            
        }
             
        
        static class EnterAction extends AbstractAction
        {
            public void actionPerformed( ActionEvent tf )
            {
              
                System.out.println( "The Enter key has been pressed." );
                menu.setVisible(false);
    			
    			menu = null;
    			myFrame.add(board);
    	        
            } // end method actionPerformed()
            
        } // end class EnterAction
        
        public static void main(String[] args) {
       	    	
            Sokoban sokoban = new Sokoban();
        
        }
    }
    And this is game, part of Board.class (not responding to pressed keyes when Menu is working) :
    Java Code:
    public Board() {
    
               	
        	rightAction = new RightAction();
            
            
            getInputMap().put( KeyStroke.getKeyStroke( "RIGHT" ), "doRightAction" );
            
            
            getActionMap().put( "doRightAction", rightAction );
        	
            
            initWorld();
            
        }
        
        static class RightAction extends AbstractAction
        {
            public void actionPerformed( ActionEvent tf )
            {
              
                System.out.println( "The Right key has been pressed." );
                            
            } 
            
        }
    When I delete Menu, it is responding to RIGHT key, but when I add menu, It is not. How to solve this problem ? Help me please. Thank you

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Java 2D game - creating menu

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ultimate is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java 2D game - creating menu

    it is my topic ... but it is not solved there and I need to solve this as soon as possible :)

  4. #4
    ultimate is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java 2D game - creating menu

    delete this please, it is solved

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Java 2D game - creating menu

    Just mark the thread as solved please. We don't delete solved posts.

Similar Threads

  1. Creating a game with java.
    By GameCoderKata in forum New To Java
    Replies: 1
    Last Post: 12-19-2011, 01:17 AM
  2. Creating a menu in a basic 2d game
    By ResidentBiscuit in forum New To Java
    Replies: 5
    Last Post: 11-30-2011, 04:18 AM
  3. Replies: 3
    Last Post: 10-18-2011, 01:04 AM
  4. Creating a Card Game in Java
    By Natrix in forum New To Java
    Replies: 1
    Last Post: 05-05-2009, 05:55 PM
  5. Replies: 3
    Last Post: 12-15-2007, 11:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •