Space Opera
Space Opera

Reputation: 3

Temporarily Disabling Mouse Input for a Window

I recently started programming in Java, and decided to start making a game, as a way to expose myself to some new concepts and methods. The game is a simple 2d turn-based strategy game (or it's going to be), and a major issue I've been struggling with is as follows:

I want all players to assign their orders simultaneously, when they have finished assigning their orders they submit/confirm/commit to their orders, but while I have the game functionally accepting orders, and a very simple "commit" command in place, I can't seem to find out how to temporarily disable user input while the game goes about moving the player's units.

I think that the problem likely lies somewhere in the following code,I apologize in advanced if the code doesn't appear in the correct format(I haven't posted a question before, but I tried):

if(Canvas.keyboardKeyState(KeyEvent.VK_SPACE)){
    isUnderway=true;
    timeStarted=System.currentTimeMillis();
}
if (isUnderway&&System.currentTimeMillis()>=timeStarted + turnTimeMillis){
    isUnderway=false;
}
if(Canvas.mouseButtonState(MouseEvent.BUTTON1)){
    targetlocationy=((mousePosition.y-25)/50)*50;
    targetlocationx=((mousePosition.x-25)/50)*50;            
}
totargetx = targetlocationx - x;
totargety = targetlocationy - y;
dist = Math.hypot( totargetx, totargety );

if(isUnderway){
    if ( dist <= 5 ){
        x = targetlocationx;
        y = targetlocationy;
    }
    else{
        x += totargetx * ( 2 / dist );
        y += totargety * ( 2 / dist );
    }
}

I have tried to alter the if() condition used to set targetlocation[x,y], by turning it into:

if(Canvas.mouseButtonState(MouseEvent.BUTTON1)&&isUnderway=false)

And several variations of that. But that code seems in-operable, I would guess that it dumps targetlocation[x/y] when isUnderway=true, resetting them to default (the current position of the piece), but seeing as it doesn't dump targetlocation[x/y] when BUTTON1 isn't held down that explanation doesn't make a lot of sense.

Currently this code functions as intended, with he exception of being able to alter the targetlocation values after orders have been committed.

Edit: This method (and the class it's in) use AWT for the GUI.

Upvotes: 0

Views: 251

Answers (1)

Xie
Xie

Reputation: 374

In SWING, thats exactly what glass panes are for.

Upvotes: 2

Related Questions