Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem reading keys  (Read 1285 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2011-12-25 05:26:51 »

Just noticed a problem in my code, when I use the same key to take input from two menus, it will re-use the input again for the next one.

Example: In my program, pressing enter opens instructions, but in the instructions page, enter goes back to the menu. Result is that it appears as if the instructions aren't coming up at all.

I am using a different key listener for the main menu and for the instructions screen, and I remove it when I switch to the other.

Here's the relevant code -

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
public void keyPressed(KeyEvent e)
      {

         if (mainMenu)
         {
            key = e.getKeyCode();
            if (key == KeyEvent.VK_UP)
            {
               if (playIsSelected)
               {
                  playIsSelected = false;
                  quitIsSelected = true;
               }
               else if (insIsSelected)
               {
                  insIsSelected = false;
                  playIsSelected = true;
               }
               else if (quitIsSelected)
               {
                  quitIsSelected = false;
                  insIsSelected = true;
               }
            }
           
            if (key == KeyEvent.VK_DOWN)
            {
               if (playIsSelected)
               {
                  playIsSelected = false;
                  insIsSelected = true;
               }
               else if (insIsSelected)
               {
                  insIsSelected = false;
                  quitIsSelected = true;
               }
               else if (quitIsSelected)
               {
                  quitIsSelected = false;
                  playIsSelected = true;
               }
            }
           
            if (key == KeyEvent.VK_ENTER)
            {
               if (playIsSelected) gameInit();
               if (insIsSelected) instructionsInit();
               if (quitIsSelected) System.exit(0);
            }
         }
         
         if (playing)
         {
            p.keyPressed(e);
         }
         
         if (instructions)
         {
            key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER) menuInit();
         }
      }


Thanks,
-Nathan

Check out my website!
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-12-25 06:13:01 »

I don't see how keyPressed could be called twice, maybe you have put this ActionListener twice on the same component. More information will probably be helpful.

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2011-12-25 19:46:13 »

Here's the source - http://www.mediafire.com/?d1sxprd5xf52s1w

p.s. Just ignore the Render class. The REAL render method is in the Main class.

Check out my website!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-12-26 04:29:17 »

Well your big mistakes is squeezing all the logic into 1 file that even I got completely confused how it runs.

Separate all the screens out into separate files. Make a Screen interface that has an update, render, keyPressed, and keyReleased methods and each class, MainMenu, Instructions, Game, implement it. Then you have 1 listener that forwards those events to the current screen.

Just some ideas to get you going Wink

Offline ReBirth
« Reply #4 - Posted 2011-12-26 05:25:14 »

Except you're doing java4k Cheesy

In my last approach, I let the main class to listen and tell current screen about keys. So in my interface class there is method for this.

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #5 - Posted 2011-12-26 05:39:37 »

Well your big mistakes is squeezing all the logic into 1 file that even I got completely confused how it runs.

Separate all the screens out into separate files. Make a Screen interface that has an update, render, keyPressed, and keyReleased methods and each class, MainMenu, Instructions, Game, implement it. Then you have 1 listener that forwards those events to the current screen.

Just some ideas to get you going Wink

I don't understand how an interface would simplify this. I haven't really used them before. Could you post some example code of what a basic version of this might look like?

Check out my website!
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-12-26 06:40:14 »

1  
2  
3  
4  
5  
6  
public interface Screen {
    public void update();
    public void render(Graphics2D g);
    public void keyPressed(int keyCode);
    public void keyReleased(int keyCode);
}


And then you game states would implement it:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public class MainMenu implements Screen {
    private Game parent;
   
    public MainMenu(Game game) {
        parent = game;
    }
   
    public void update() {
        ...
    }

    public void render(Graphics2D g) {
        ...
    }

    public void keyPressed(int keyCode) {
        if(keyCode == KeyEvent.VK_ENTER)
            parent.setScreen(new Instructions());
    }
   
    public void keyReleased(int keyCode) {
        ...
    }
}

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #7 - Posted 2011-12-26 07:23:32 »

Thanks. I don't quite understand how the rendering would work when the rendering is being done in multiple different methods, though.

Check out my website!
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-12-26 07:24:15 »

The rendering is all done by the Graphics2D object so all you need to do is pass it around.

Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #9 - Posted 2011-12-27 06:07:01 »

If you were doing it the way we talked about in another post of yours, with a boolean value like keyEnter:  You can make it a non-repeating key.  In the part of the program where you check to see if keyEnter is true (set by keyPressed and keyReleased), if it is true then immediately set it to false and carry on, then the next time you check to see if it is true it won't be unless you have released and pressed it again.

... Also I wouldn't use multiple key listeners.  Just have one that sets the boolean variable representing that key and use your main/menu loops to work with them.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #10 - Posted 2011-12-29 06:32:54 »

Could you post the instructionsInit() method and the Instruction screen code?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (9 views)
2014-07-29 12:53:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (23 views)
2014-07-29 03:07:13

E.R. Fleming (9 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!