Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  [Solved] Keypress window algorithm  (Read 1099 times)
0 Members and 1 Guest are viewing this topic.
Offline CogWheelz
« Posted 2014-08-12 00:49:14 »

For the past hour, I've been trying to create a way to make a window pop up on keypress and and disappear once pressed again. I'm using libGdx and here's what I've been trying.

1  
2  
boolean windowOpen = false;
int windowY = 1000;


In my render method:
1  
2  
if(windowY < 1000)
            batch.draw(window, 0, windowY);


Update method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
 if(Gdx.input.isKeyPressed(Keys.C) && !pressedC && windowY < 1000){
         pressedC = true;
         windowOpen = true;
         windowY = 1000;
         
         
      }
     
      if(!Gdx.input.isKeyPressed(Keys.C) && pressedC){
         pressedC = false;        
      }
     
     
     
      if(windowOpen)
         windowY = 1000;
     
     
      if(windowY >= 1 && !windowOpen) {
         windowY-= 10;
      }


I spent too much time and am loosing it  Emo , might as well make it disappear once Escape is pressed;
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #1 - Posted 2014-08-12 01:01:00 »

Polling input state in the game loop is quite fragile and convoluted for anything more than trivial (as you have found out), you should really upgrade to observers:

https://github.com/libgdx/libgdx/wiki/Event-handling
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// use a multiplexer so that we can listen for more than just "C pressed" events
InputMultiplexer multiplexer = new InputMultiplexer();
Gdx.input.setInputProcessor(multiplexer);


multiplexer.addProcessor(new InputAdapter () {
    @Override
    public boolean keyDown(int keycode) {
        if (keycode == Keys.C) {
            toggleWindowState();
            return true;
        }
        return false;
    }
});

// because we're using a multiplexer, you can add other processors here


You can implement toggleWindowState() any way you want, but I do recommend abstracting it out of the event listener into it's own method.
Offline CogWheelz
« Reply #2 - Posted 2014-08-12 01:32:02 »

Also, is there a way I can keep the x and y the same when the window is re-sized?

Example:
If the mouse is clicked at a certain x or y, an action is performed. That x and y moves when the window is re-sized. should I just prevent re-sizing?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #3 - Posted 2014-08-12 01:37:25 »

Are you talking about the application window or some "window" object that is presumably some kind of texture? (
batch.draw(window, 0, windowY);
)

I guess what I mean is: What are you trying to do?
Offline CogWheelz
« Reply #4 - Posted 2014-08-12 01:44:39 »

I need this to work when the user resizes the window;
1  
2  
3  
4  
5  
6  
if(Gdx.input.getY() < 223 && Gdx.input.getY() > 184) {
          if(Gdx.input.getX() < 311 && Gdx.input.getX() > 87)
             if (Gdx.input.isButtonPressed(0)){
                 ready = true;  
                 }
}
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 273
Exp: 5 years



« Reply #5 - Posted 2014-08-12 01:48:17 »

You're hardcoding numbers and not abstracting/encapsulating, both of which will prove harmful once you do it with more than 1 thing.

It looks like you want a Button.
More info on using the excellent Scene2d ui.
Offline CogWheelz
« Reply #6 - Posted 2014-08-12 04:17:15 »

Aww, more libraries :/. (look at my profile Tongue)
Online Longarmx
« Reply #7 - Posted 2014-08-12 04:30:27 »

Aww, more libraries :/. (look at my profile Tongue)

But... Why? Sad

Offline CogWheelz
« Reply #8 - Posted 2014-08-12 15:25:17 »

I appreciate libraries, but I'm not in the mood to learn so much more at once. That would be another big trial and error that i could face after I make my game, if I make any sense.
Offline Roquen
« Reply #9 - Posted 2014-08-12 20:23:41 »

Then don't poll and process events.
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (57 views)
2014-11-26 15:22:04

toopeicgaming1999 (50 views)
2014-11-26 15:20:36

toopeicgaming1999 (10 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (26 views)
2014-11-25 11:26:43

Gibbo3771 (23 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (75 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!