在java中多次为函数使用延迟

问题描述

我正在尝试创建 Pacman 游戏。鬼在每轮开始时都有这样的限制,每次回到家中(吃过吃豆子或被吃豆子吃掉后),都必须在那里停留一段时间。我尝试了一些方法,例如 Timer 和 TimerTask 和 scheduledexecutorservicea。但一切都无济于事! 我的问题是延迟仅在第一次有效 - 使用两种方法 - 即使我每次都重新实例化调度程序不起作用。我正在寻找一种方法,让它在前面提到的条件之后每次都延迟。

提前感谢您的帮助和关注。

是我定义每个移动一个幽灵的可运行对象的地方。

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;
    }
};

在这里我在一个函数中运行它们

    public void moveGhosts() {
    if (ghost1Hasstopped) {
        scheduledexecutorservice ghost1Scheduler = new ScheduledThreadPoolExecutor(1);
        ghost1Scheduler.scheduleWithFixedDelay(ghost1Runnable,5,TimeUnit.SECONDS);
    }
    else
        ghost1Runnable.run();
    if (ghost2Hasstopped) {
        scheduledexecutorservice ghost2Scheduler = new ScheduledThreadPoolExecutor(1);
        ghost2Scheduler.scheduleWithFixedDelay(ghost2Runnable,TimeUnit.SECONDS);
    }
    else
        ghost2Runnable.run();
    if (ghost3Hasstopped) {
        scheduledexecutorservice ghost3Scheduler = new ScheduledThreadPoolExecutor(1);
        ghost3Scheduler.scheduleWithFixedDelay(ghost3Runnable,TimeUnit.SECONDS);
    }
    else
        ghost3Runnable.run();
    if (ghost4Hasstopped) {
        scheduledexecutorservice ghost4Scheduler = new ScheduledThreadPoolExecutor(1);
        ghost4Scheduler.scheduleWithFixedDelay(ghost4Runnable,TimeUnit.SECONDS);
    }
    else
        ghost4Runnable.run();
}

这里我重置了延迟条件:

public void resetAllGhostsDelay() {
    canGhost1EatOrBeEaten = false;
    canGhost2EatOrBeEaten = false;
    canGhost3EatOrBeEaten = false;
    canGhost4EatOrBeEaten = false;
    ghost1Hasstopped = true;
    ghost2Hasstopped = true;
    ghost3Hasstopped = true;
    ghost4Hasstopped = true;
}

一个幽灵被吃掉时,我也会这样做:

    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;
    }
}

我使用 TimerTask 和 Timer 做了类似的事情,但也不起作用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)