I have been making a mario game and have made good progress. Now I need to switch between worlds. First I stop the thread running my update and draw methods, then I remove everything that is in the world(player , enemies , grass , etc.) and then I load a new world. Then I try to start the thread again. But for some reason after stopping the thread, nothing is executed after that and it just 'freezes' right there.
private synchronized void clearWorld() {
stop();
System.out.println("Stopped");
for(int a = 0 ; a < handler.wall.size() ; a++) handler.wall.remove(handler.wall.get(a));
for(int b = 0 ; b < handler.creature.size() ; b++) handler.creature.remove(handler.creature.get(b));
System.out.println("Everything removed");
}
private synchronized void switchWorld(String path) {
world = new World(this , path);
start();
System.out.println("Thread started");
}
public synchronized void stop() {
if(!running) return ;
running = false ;
try {
Main.getGame().thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void start() {
if(running) return ;
running = true ;
Main.game.thread.start();
}
public void run() {
init();
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
if(world.Goombas==getPlayer().gKilled ) {
clearWorld();
switchWorld("/pipe_world1.txt");
}
timer += 1000;
System.out.println(updates + " Ticks, Fps " + frames);
updates = 0;
frames = 0;
}
}
}