[Solved] Keypress window algorithm

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.

boolean windowOpen = false;
int windowY = 1000;

In my render method:

if(windowY < 1000)
      		batch.draw(window, 0, windowY);

Update method:

 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;

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:


// 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.

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?

Are you talking about the application window or some “window” object that is presumably some kind of texture? ([icode]batch.draw(window, 0, windowY);
[/icode])

I guess what I mean is: What are you trying to do?

I need this to work when the user resizes the window;

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;  
    		       }
}

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.

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

But… Why? :frowning:

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.

Then don’t poll and process events.