JLK
JLK

Reputation: 787

android -- wait for user input

I'm creating a spades app with 1 human player and 3 computer players.

The problem that I am having is, the play must happen sequentially (clockwise) and I need my program to wait on the player input. I can't use wait() and notify(). I have tried while loops to check whether the user has selected a card but those stop the program from running. I've tried recursive methods that won't return anything until the player has chosen a card. That too does not work. So what do I do? I'm stuck.

My method goes like this (leaving out the non-pertinent code)

private void game(){
   while(there are more tricks to be played)
      while(each player has not played){
         if(human turn)
            get input from player
         else
            computer plays
      }
}

Upvotes: 0

Views: 725

Answers (3)

Bogdan Alexandru
Bogdan Alexandru

Reputation: 5552

Maybe you should change a little bit your game controller. Instead of waiting for anything, have your program continuously paint the screen. If user inputs nothing, same screen is paint all the time.

Once he presses a key (or clicks a card or whatever events you have), simply modify the paint method's argument (the screen to be painted). Thus you will separate painting the screen and input handling. It's a common technique called MVC model.

Maybe this will help (it's my game creating blog, and the links inside it are also helpful): http://m3ph1st0s.blogspot.ro/2012/12/create-games-with-libgdx-library-in.html

You don't need to adapt all your game to the game described there, only the technique of separating input from painting. As a general technique, you should NOT have input determine painting.

Hope it helps. Don't hesitate to request further help.

Upvotes: 2

Noby
Noby

Reputation: 6602

Try this.

Create one thread and in that threat call sleep(1000);

Upvotes: 1

Saqib Razaq
Saqib Razaq

Reputation: 874

You can try to add Event Handlers. It will try triger a event every time the user selects a card.

Upvotes: 1

Related Questions