Is there a way to make my 2048 game run faster?
I made a setting called high speed mode so that if moves were made really fast I would just skip the spawning, combining and moving animations, however even with that I am still usually one or two moves behind when I spam a bunch of keys at once.
Right now I have a tilePanel class to handle displaying my tiles, and use a timer(very basic code) with paintComponent(drawing the tiles constantly) to make sure it continually updates the board.
public void doAnimation() {
System.nanoTime();
Timer timer = new Timer(2, this);
timer.start();
}
The way I have structured my game is that I have an ArrayList of commands(ie pressing the right key adds "right" to the arraylist and eventually the arraylist gets to it and executes) I receive to make sure that it always does a move, spawns before doing the next move in the sequence.
To make sure the board continually updates itself, i made a very basic run thread:
public void run(){
while(true) {
if(canDoNewMove&&commands.size()>0&&parent.spawnsLeft==0) {
move(commands.get(0));
commands.remove(0);
canDoNewMove=false;
}
if(commands.size()>=2) {
board.isBehind=true;
}
else {
board.isBehind=false;
}
if(totalMovements==0&&moveDone) {
moveDone=false;
if(parent.changed) {
parent.spawn(parent);
}
canDoNewMove=true;
}
//moveDone=false;
try {
Thread.sleep(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}};
Thread sampleThread = new Thread(backGroundRunnable);
sampleThread.start();
}
Would reducing method calls produce a noticeable enough difference that the actions would finish right after I press the keys or is the problem within the tilePanel class that draws the board?
thanks guys