• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple wait with a while loop blocking the whole app  RSS feed

 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so I'm working on a game application and I need the player to move an object on the screen before the games continues if not the game should just sit there forever...but using a while loop like so

upon arriving at this point the game freezes and I get the mouse rotating on end thing on the windows machine I'm using and I see something like "program not responding"

so changing it to this and handling exceptions ...


I get the same result

I just need the function to pause right there till getCount() value get increased...
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
need the player to move an object on the screen before the games continues

Normally that is done by the program letting the JVM do the waiting until a user created event.  When the player moves, that creates an event the JVM will pass to a listener in the code where the the game can continue.
Do not use calls to sleep to do any waiting on user actions.

I am not familiar with JavaFX so can not recommend techniques to use there.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
need the player to move an object on the screen before the games continues

Normally that is done by the program letting the JVM do the waiting until a user created event.  When the player moves, that creates an event the JVM will pass to a listener in the code where the the game can continue.
Do not use calls to sleep to do any waiting on user actions.

I am not familiar with JavaFX so can not recommend techniques to use there.


this is quite strange, I just want all other objects executing other functions to continue to do their thing, like the one playing the game music, that must not stop...but the function/thread where the while loop is should stop...
soo strange..I'm at a loss...
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more; Threa‍d.sleep() does not look right when used for GUIs. Although FX doesn't run on the event dispath thre‍ad like Swing, there is a concept of having to run on a particular Th‍read. That is why you start off all FX Applications with launch().
I would have thought that your display will wait for input regadrless, and you wouldn't need to make anything sleep.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try
I have a space ship made of cells called sectors from my class Sector which I designed with  Shape Polygon.
I placed my ship in a Group I called tileGroup.
I have a class player which creates number of players after asking user to input number of players...
I have a Piece class which creates a circular piece using Shape Circle for each player..a player can be an alien or a human, all pieces are placed in the tileGroup, which will be set in the center of a BorderPane...
I have a game class which will control the players playing the game, through it's method void gaming()..
at the top of the BorderPane there is an hbox containing rectangle shapes which will in certain conditions will take mouse inputs... //lol is getting complicated eh...


then I create content and pass is to Stage and I show like this

running this everything goes fine and the app shows up and the game is ready to be played..

OK..so the game must be played in a series of rounds..TO TRACK the movement of a player piece, class piece has a field called count and when a player moves a piece with the mouse, count must be increased...
hence...


so this is a bit more detailed to give a more clearer idea..hopes someone can explain why this situation happens...meanwhile I'm gonna try to implement that point in a different way..
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, is this gaming method running on a different Th‍read? Why do you want to sleep? I can't see the point of that. Is that so as to introduce a delay between the human player's turn and the computer's? If not, it may cause the application to hang; the player can never make a move.
It is hard to work out how you are calculating turns. How many players are there? Is there a need for the game to count how many turns have been taken and each player to count as well?
Why have you got a label in line 3?
Your indentation is inconsistent: don't put line end comments or actual code on the same line as a { because that makes the code hard to read. (Lines 4 and 6.)
Don't try equals(null) (line 10); it ought always to return false. If it ever is null, you will get an Exception before you get into that equals() call. I think you mean something like
Why do you have a public constant ALIEN? That looks strange. Is PlayerCharacter an enum (=enumerated type)? In which case it is all right to have ALIEN as a public constant, but you can then use the == operator; it even says so in the Java® Language Specification (=JLS). Beware: The JLS can be very hard to read.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by pressing a botton in hbox the method void gaming() is called..so it does not start straight away after building the space ship..
I want to sleep because I have to force the player to move. That is how the game works, is it played in turns for 39 round..so if is the turn of a player, till the player moves his piece the game must WAIT/SLEEP..
I have a lable because in certain conditions I can break the whole loop and end the game...PlayerCharacter.ALIEN is an enumeration...
damn it campbell Ritchie...you focusing on all the things which are not why I posted and never talked once about the problem I asked...I'm the one designing the game so I know what I have to do, I know...I'm having a particular problem..talk about that ...
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you take your car for an oil change, the mechanic will notice and warn you if you have a tyre with no tread. He would not be doing his job if he didn't warn you.

I don't think you have a principally FX problem, so I shall try duplicating the discussion in other fora, and you might get more attention.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's normal in most GUI systems that, if you cause the GUI's thread to sleep, that blocks the whole GUI. I don't know specifically about Java FX but I can tell you that's how it works in Swing.

So you're going to have to recast your code so that it works according to the way that GUI's want to work. You already have a requirement which says something like "When the user clicks on X then such and such should start happening"; that suggests that you should have a listener attached to X which tells you when the user clicks there. When that happens, your code should cause that other stuff to start happening.
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It's normal in most GUI systems that, if you cause the GUI's thread to sleep, that blocks the whole GUI. I don't know specifically about Java FX but I can tell you that's how it works in Swing.

So you're going to have to recast your code so that it works according to the way that GUI's want to work. You already have a requirement which says something like "When the user clicks on X then such and such should start happening"; that suggests that you should have a listener attached to X which tells you when the user clicks there. When that happens, your code should cause that other stuff to start happening.


I think this is the solution, creating a listener..so if something is clicked...something happens..if something is moved..something happens..if not nothing happens...this will really solve it...thanks Paul Clapham..I was seeking the lazy way out with that while loop.. ;)
 
John Damien Smith
Ranch Hand
Posts: 357
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The generic solution is to use event driven programming.

You shouldn't have sleeps or never ending loops on the JavaFX application thread, for reasons already covered in previous posts.

Specific to JavaFX, event handlers are used.
Often, in JavaFX programming, the event handlers will set properties.
Properties are slightly different from standard Java object getters and setters in that you can add listeners to the properties that will be triggered when the properties change.

So that gives you two options for responding to things, either directly by using event handlers attached to GUI controls and nodes OR indirectly by reacting to changes to properties that were updated by event handlers.

So, two simple ways to accomplish waiting for a player to take a turn are:
1. Just have a button on the screen which says next turn, set the onAction event handler to trigger the logic you want to take effect when the user takes a turn.  If you don't want a button but some other interaction to end a turn, (such as the player dragging a space ship to a sector), then another appropriate event can be used, for example drag and drop event handlers.
2. You can set the logic of ending the turn based upon the state of the application.  For example, a Game object might have multiple player objects and a property representing which players turn it is .  An event handler such as defined above in (1) can update the game state by notifying the game that the current players turn has completed, the game can work out who the next player is and set the currentPlayer to the next player due to take a turn.  Any UI objects that need to update based upon the currentPlayer (for example highlight the name of the current player), can use a change listener on the currentPlayer property to see when it changes and update themselves accordingly.

Note, depending upon what you want to do, you might only need to code (1) and not (1 & 2).

=====

Note, the info below is a bit more advanced and not necessarily applicable to what you wish to do.

A model, view, controller approach can also be employed if you wish.  The view and controller can be an FXML loaded document with its associated controller or some Java code which codes the graphics shapes and controls.  The model can be a class which contains properties which represent the application state and are set and updated as appropriate.  For a large program, you can have shared model objects accessed through multiple views and controllers.  Note that explicit definition of a model view controller system is not necessary for a very simple application as such an application can represent state directly in the controls (e.g. text in a text area or check state of a radio button), and react purely based upon event action handlers defined by the controls.  It is only for larger apps or library components like controls that explicit models may become useful as they can be separated from the GUI and tested independently, concentrating domain logic inside the model and controllers and separated from the GUI code.  See the JavaFX UI controls architecture and source code for a button for an example of this. 
 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem solved! I solve the whole thing using the java fx concurrent package found here
http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

 
John Damien Smith
Ranch Hand
Posts: 357
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using javafx concurrent facilities to solve the problem you outline in your question is an unusual solution.  Usually JavaFX concurrent is only necessary if you have extremely long computations (rare) or (more commonly) a lot of disk or network I/O.  Neither of which seemed to be required in from your question (unless it is a multi-player network based game).  Regardless, good that you got a solution that you are happy with.  If it works for you that's great.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!