0

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Which GUI library are you using? AWT? Swing? – Hovercraft Full Of Eels Jun 29 '13 at 16:46
  • Sorry, I'm using AWT in this class. I'll add it to my question post. – Space Opera Jun 29 '13 at 16:47
  • 1
    It almost seems as if you are mixing both Swing and AWT in the same program. If so, re-consider this as there is no need to use AWT, a library that is much more limited than Swing when you could create an all-Swing GUI. – Hovercraft Full Of Eels Jun 29 '13 at 16:53
  • 1
    1) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). 2) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 29 '13 at 16:56
  • I am using both, and I had started to see that was going to become an issue, but since I'm still rather inexperienced with java I was hoping I could finish this program, or at least get this part working, and then rebuild it using only swing. Most of my early work in this program was influenced by a rather... poor set of tutorials, really a case of the blind being lead by the nearly blind. – Space Opera Jun 29 '13 at 16:58
  • @SpaceOpera: you really want to get rid of the AWT code and use all-Swing (or JavaFX I suppose, though I'm not that familiar with it). If you mix AWT and Swing without a good reason and without the skill and knowledge of how to take care to avoid the pitfalls of the mix, you're in for trouble. – Hovercraft Full Of Eels Jun 29 '13 at 17:00

1 Answers1

2

In SWING, thats exactly what glass panes are for.

Xie
  • 374
  • 1
  • 8
  • The Class used to draw the Window for this program uses SWING, would it be as simple as setting the glass pane to be on top while isUnderway=true and then moving it back to default position when isUnderway=false? – Space Opera Jun 29 '13 at 16:52
  • Thanks! This should make this substantially quicker/easier. – Space Opera Jun 29 '13 at 17:02