LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: lokopepe on May 18, 2013, 22:59:42

Title: problem with Mouse.isButtonDown
Post by: lokopepe on May 18, 2013, 22:59:42
I'm trying to write an UI for a game being as modular as possible
So I separated the game states in classes, made a class for the state machine, and the main class
the way my code works is like that:
main calls
while (!displayWindow.isCloseRequested()){
displayWindow.clear();
stateMachine.run();
displayWindow.update();
}


state machine's "run()" runs the render and inputcheck, so it does:
switch(state)
case State.X:
stateX.draw();
if(Mouse.isButtonDown(0) && stateX.buttonY.inBounds(Mouse.getX(), 600 - Mouse.getY()) )
this.changeState(State.W);

break;
case State.W
stateW.draw();
if(Mouse.isButtonDown(0) && stateW.buttonK.inBounds(Mouse.getX(), 600 - Mouse.getY()) )
  System.exit(0);
break;


the problem is, buttonK in state W is in the same position as buttonY in state X, so when I click buttonY in state X, it changes to state W and close the window.
I fixed this issue by initializing the state classes when the state changes (instead of initializing all of them at the beginning) , however, it lags a lot when changing states

so my question is, what I'm doing wrong in this case?
Mouse.isButtonDown should stay true for only one iteration, right? it's not that is changing the state faster than I release the mouse button, cause I tested forcing the thread to sleep for a second when changing the state, it waits then close the window.
Title: Re: problem with Mouse.isButtonDown
Post by: quew8 on May 19, 2013, 00:05:18
Use the event buffer of Mouse.


while(Mouse.next()) {
    if(Mouse.getEventButton() == 0 && Mouse.getEventButtonState()) {
        //Then mouse button 1 has been pressed
    }
}



It's all in the wiki: http://www.lwjgl.org/wiki/index.php?title=LWJGL_Basics_2_(Input) (http://www.lwjgl.org/wiki/index.php?title=LWJGL_Basics_2_(Input)) there's a link to the java doc there but the methods are pretty self-explanatory if you look.
Title: Re: problem with Mouse.isButtonDown
Post by: lokopepe on May 19, 2013, 01:26:53
thanks, it's working correctly now