i have programmed java game where only one player kann play and hit the enemy an then i want to make it turn-based game.
I recherched in the internet but i don’t find any help. Hier is a detail to my code (see below).
I used the java.util.Timer class to create Turn-Based Game but it doesn’t work
correctly. I tested it with 2 player. the first player get 1000 ms time but the second only 100 ms.
is my method correct?
Can you tell me wehre can i find help or better concept, tutorials etc to make my game turn-based.
Thanks!
1)GamePanel.java
void init() {
…
new GameLevel1State();
…
running = true;
}
public void run() {
init();
// Timers
long start;
long elapsed;
long wait;
// Game loop
while (running) {
start = System.nanoTime();
update();
draw();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
if (wait < 0)
wait = 0;
try {
Thread.sleep(wait);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2)GameLevel1State.java
…
…
Timer timer;
public GameLevel1State(…) {
…;
init();
}
public void init() {
…
…
timer = new Timer();
timer.schedule(new TurnBasedTask(), 0,1000);
}
class TurnBasedTask extends TimerTask {
public void run() {
setActiveSpieler();
}
}
}