Reputation: 1
I'm trying to create a Pacman game. The ghosts have such a restriction at the beginning of each round and each time they return to their home (after eating Pacman or being eaten by it), that have to stay there for a certain period of time. I've tried some methods such as Timer and TimerTask and ScheduledExecutorServicea. But all to no avail! My problem is the delay works only the first time -with both methods- and even though I re-instantiate the schedulers each time it doesn't work. I'm looking for a way to make it delay each time after the previously mentioned conditions.
Thanks for your help and attention in advance.
is where I define the runnables that each move a ghost.
private Runnable ghost1Runnable = new Runnable() {
@Override
public void run() {
Point2D[] ghost1Coordinates = moveThisGhost(ghost1Location, ghost1Velocity);
ghost1Location = ghost1Coordinates[0];
ghost1Velocity = ghost1Coordinates[1];
ghost1HasStopped = false;
canGhost1EatOrBeEaten = true;
}
};
private Runnable ghost2Runnable = new Runnable() {
@Override
public void run() {
Point2D[] ghost2Coordinates = moveThisGhost(ghost2Location, ghost2Velocity);
ghost2Location = ghost2Coordinates[0];
ghost2Velocity = ghost2Coordinates[1];
ghost2HasStopped = false;
canGhost2EatOrBeEaten = true;
}
};
private Runnable ghost3Runnable = new Runnable() {
@Override
public void run() {
Point2D[] ghost3Coordinates = moveThisGhost(ghost3Location, ghost3Velocity);
ghost3Location = ghost3Coordinates[0];
ghost3Velocity = ghost3Coordinates[1];
ghost3HasStopped = false;
canGhost3EatOrBeEaten = true;
}
};
private Runnable ghost4Runnable = new Runnable() {
@Override
public void run() {
Point2D[] ghost4Coordinates = moveThisGhost(ghost4Location, ghost4Velocity);
ghost4Location = ghost4Coordinates[0];
ghost4Velocity = ghost4Coordinates[1];
ghost4HasStopped = false;
canGhost4EatOrBeEaten = true;
}
};
and here I run them in a function
public void moveGhosts() {
if (ghost1HasStopped) {
ScheduledExecutorService ghost1Scheduler = new ScheduledThreadPoolExecutor(1);
ghost1Scheduler.scheduleWithFixedDelay(ghost1Runnable, 5 ,5 , TimeUnit.SECONDS);
}
else
ghost1Runnable.run();
if (ghost2HasStopped) {
ScheduledExecutorService ghost2Scheduler = new ScheduledThreadPoolExecutor(1);
ghost2Scheduler.scheduleWithFixedDelay(ghost2Runnable, 5 ,5 , TimeUnit.SECONDS);
}
else
ghost2Runnable.run();
if (ghost3HasStopped) {
ScheduledExecutorService ghost3Scheduler = new ScheduledThreadPoolExecutor(1);
ghost3Scheduler.scheduleWithFixedDelay(ghost3Runnable, 5 ,5 , TimeUnit.SECONDS);
}
else
ghost3Runnable.run();
if (ghost4HasStopped) {
ScheduledExecutorService ghost4Scheduler = new ScheduledThreadPoolExecutor(1);
ghost4Scheduler.scheduleWithFixedDelay(ghost4Runnable, 5 ,5 , TimeUnit.SECONDS);
}
else
ghost4Runnable.run();
}
and here i reset the conditions for delay:
public void resetAllGhostsDelay() {
canGhost1EatOrBeEaten = false;
canGhost2EatOrBeEaten = false;
canGhost3EatOrBeEaten = false;
canGhost4EatOrBeEaten = false;
ghost1HasStopped = true;
ghost2HasStopped = true;
ghost3HasStopped = true;
ghost4HasStopped = true;
}
and i aslo do this eah time a single ghost is eaten:
public void returnThisGhostToInitialLocation(Cell ghost) {
if (ghost == Cell.GHOST_1_HOME) {
ghost1Location = new Point2D(ghost1InitialLocation.getX(), ghost1InitialLocation.getY());
ghost1HasStopped = true;
}
else if (ghost == Cell.GHOST_2_HOME) {
ghost2Location = new Point2D(ghost2InitialLocation.getX(), ghost2InitialLocation.getY());
ghost2HasStopped = true;
}
else if (ghost == Cell.GHOST_3_HOME) {
ghost3Location = new Point2D(ghost3InitialLocation.getX(), ghost3InitialLocation.getY());
ghost3HasStopped = true;
}
else if (ghost == Cell.GHOST_4_HOME) {
ghost4Location = new Point2D(ghost4InitialLocation.getX(), ghost4InitialLocation.getY());
ghost4HasStopped = true;
}
}
I've done something similar using TimerTask and Timer but that doesn't work either.
Upvotes: 0
Views: 118