Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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  
  [LibGDX] Using the same input key in a pause and play state  (Read 2914 times)
0 Members and 1 Guest are viewing this topic.
Offline GamerIDGoesHere

Junior Devvie


Exp: 7-9 months



« Posted 2015-01-27 01:09:37 »

Hi,

I'm attempting to create a Pause state for my game, however its not actually a state, its just an overlay (to make it easier to resume) but the problem I'm having is when you press the escape key, it goes into the pauseState and then when you try to go back to the play state it immediately pauses again because the escape key is how you enter and exit it. I need to know how to make it so that when you press the escape key, it will only register the press once and then ignore that press in the new state.

(At the moment the game is mostly based off of ForeignGuyMike's Block Bunny tutorial)

This is a snippet of the GameStateManager 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  
   private PauseState pauseState;
   public boolean paused;

   public void update(float dt) {
      gameStates.peek().update(dt);
      if(paused) {
         pauseState.update(dt);
         System.out.println("updated!");

         return;
      }
   }
   
   public void render() {
      gameStates.peek().render();
      if(paused) {
         System.out.println("paused gsm");
         pauseState.render();

         return;
      }
   }
   
   public void setPaused(boolean b) { paused = b; System.out.println("this works");}


This is the input for the pausestate code (It enters the pausestate fine, but when you press escape again it flashes back to play state and then back to the pausestate)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   public static boolean pEscape = false;
   public void handleInput() {
         if(Play.escape) {
            pEscape = false;
         }  
         if(SJInput.isPressed(SJInput.ESCAPE)) {
            pEscape = true;
         }
         
         if(pEscape) {
            gsm.setPaused(false);
         }

   public void update(float dt) {
         handleInput();
   }


And here is the play code to enter the pauseState

1  
2  
3  
4  
5  
6  
7  
8  
   public static boolean escape = false;

         if(SJInput.isPressed(SJInput.ESCAPE)) {
            escape = true;
         }
         if(escape) {
            gsm.setPaused(true);
         }


Any ideas would be greatly appreciated. (I've tried taking it up with Mike, and he's been quite helpful, but I don't want to keep annoying him with my constant barrages of questions Tongue)

Thanks
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2015-01-27 01:20:03 »

Use an InputProcessor to capture input events instead of polling their state each frame.
Offline GamerIDGoesHere

Junior Devvie


Exp: 7-9 months



« Reply #2 - Posted 2015-01-27 01:23:39 »

Use an InputProcessor to capture input events instead of polling their state each frame.

Yeah I'm using these from the tutorial, but still with the same issue, because I'm not changing state, but putting an overlay on top.
http://pastebin.java-gaming.org/7d50a68901915

http://pastebin.java-gaming.org/d50a870991518


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #3 - Posted 2015-01-27 07:18:32 »

If game is paused don't update game states, only paused state.
Offline craftm

JGO Coder


Medals: 15
Projects: 1


_Keep Trying


« Reply #4 - Posted 2015-01-28 20:23:21 »

1 - You can use Gdx.input.isKeyJustPressed.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
switch (gameState)
{
   case isPlaying:
      if (Gdx.input.isKeyJustPressed(Keys.ESCAPE))
         gameState = isPaused;
   break;
   case isPaused:
      if (Gdx.input.isKeyJustPressed(Keys.ESCAPE))
         gameState = isPlaying;
   break;
}


2 - You can use a custom InputProcessor to set keydown/keyup:

GameInputProcessor
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public class GameInputProcessor extends InputAdapter
{
   public boolean keyDown (int k)
   {
      if (k == Keys.ESCAPE)
         GameKeys.setKey (GameKeys.ESCAPE, true);
      return true;
   }

   public boolean keyUp (int k)
   {
      if (k == Keys.ESCAPE)
         GameKeys.setKey (GameKeys.ESCAPE, false);
      return true;
   }
}


GameKeys
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  
public class GameKeys
{
   private static boolean[] keys;
   private static boolean[] pKeys;
   private static final int numKeys = 1;
   public static final int ESCAPE = 0;
   
   static
   {
      keys = new boolean[numKeys];
      pKeys = new boolean[numKeys];
   }
   
   public static void update()
   {
      for (int i=0; i< numKeys; i++)
      {
            pKeys[i] = keys[i];
      }
   }
   
   public static void setKey (int k, boolean b)
   {
      keys[k] = b;
   }
   
   public static boolean isDown (int k)
   {
      return keys[k];
   }
   
   public static boolean isPressed(int k)
   {
      return keys[k] && !pKeys[k];
   }
}


Create
1  
Gdx.input.setInputProcessor(new GameInputProcessor());


Render
1  
2  
if (GameKeys.isDown(GameKeys.ESCAPE))
   //some code

_ Wink
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (579 views)
2019-09-04 15:33:17

hadezbladez (5497 views)
2018-11-16 13:46:03

hadezbladez (2398 views)
2018-11-16 13:41:33

hadezbladez (5757 views)
2018-11-16 13:35:35

hadezbladez (1220 views)
2018-11-16 13:32:03

EgonOlsen (4659 views)
2018-06-10 19:43:48

EgonOlsen (5680 views)
2018-06-10 19:43:44

EgonOlsen (3196 views)
2018-06-10 19:43:20

DesertCoockie (4094 views)
2018-05-13 18:23:11

nelsongames (5113 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!