访问计时器内的对象:Java

访问计时器内的对象:Java,java,Java,我需要一些关于我目前正在进行的项目的帮助。这是一款植物大战僵尸的游戏,我一直在添加僵尸 Timer ZombieTimer = new Timer (); TimerTask task = new TimerTask() { //for continuous arrival of zombies public void run() { Random rand = new Random(); //puts a zombie in a rand

我需要一些关于我目前正在进行的项目的帮助。这是一款植物大战僵尸的游戏,我一直在添加僵尸

  Timer ZombieTimer = new Timer (); 

    TimerTask task = new TimerTask() { //for continuous arrival of zombies
      public void run()
      {
        Random rand = new Random(); //puts a zombie in a random lane
        int a = rand.nextInt(4);
        int b=9; 

        Zombie z = new Zombie(); 
        gardenField[a][b] = z;
        System.out.println("New zombie in " + a + " tile " + b);
      }
    };

    ZombieTimer.scheduleAtFixedRate(task, 1000, 8000); //new zombie every 8 seconds

现在,在创建一个僵尸对象之后,我想让僵尸在它所属的水平阵列中移动,移动到离植物更近的位置。我也在考虑使用计时器,但我不知道是否应该在Zombie类中传递整个数组。有什么帮助吗?谢谢。

我认为你走的路是对的,但是你没有必要把清单交给计时器。在任务的run方法中,您只需调用一个方法updatePositions,该方法将僵尸向前移动1步,这是在外部类中定义的。

我认为您走的是正确的道路,但无需将列表传递给计时器。在任务的run方法中,您只需调用一个方法updatePositions,该方法将僵尸向前移动1步,这是在外部类中定义的。

您不需要将数组字段传递给僵尸类


如果你有权访问gardenField的某个地方,那么只需在每个x时间间隔更新它们,方法是循环数组中的所有僵尸并将它们的位置向右移动。为此创建一个单独的计时器,它应该可以工作。

您不需要将数组字段传递给Zombie类


如果你有权访问gardenField的某个地方,那么只需在每个x时间间隔更新它们,方法是循环数组中的所有僵尸并将它们的位置向右移动。为此创建一个单独的计时器,它应该可以工作。

我会使用ScheduledExecutorService而不是计时器。另外,我会使用ExecutorService让一个线程负责移动僵尸。 例如:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class ZombiesVsPlantsExample {

private static volatile Zombie[][] map = new Zombie[100][100]; // volatile not needed if you synchronize all access to the map. not only writes

    public static void main(String[] args) {
        ZombiesVsPlantsExample zombiesVsPlantsExample = new ZombiesVsPlantsExample();
        zombiesVsPlantsExample.doTheWork();
    }

    private void doTheWork() {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);  // this thread pool will be used to create zombies

        ExecutorService executorService = Executors.newFixedThreadPool(1);  // this thread pool will be used to move zombies



        ZombieCreator zombieCreator = new ZombieCreator(map);
        scheduler.scheduleAtFixedRate(zombieCreator, 2, 8, TimeUnit.SECONDS); // one new zombie every 8 seconds

        executorService.submit(new ZombiesPositionProcessor(map));

    }
}

class Zombie {

}

class ZombieCreator implements Runnable {

    Zombie[][] map;

    public ZombieCreator(Zombie[][] map) {
        this.map = map;
    }

    @Override
    public void run() {
        Zombie zombie = new Zombie();
        synchronized(map){
            map[1][2] = zombie;  // put new zombie in some random location in map
        }
        System.out.println("new zombie was created");
    }
}

class ZombiesPositionProcessor implements Runnable {

    Zombie[][] map;

    public ZombiesPositionProcessor(Zombie[][] map) {
        this.map = map;
    }

    @Override
    public void run() {
        while (true) {
            // iterate map and move zombies one by one
            System.out.println("moving one zombie");
        }
    }
}

我会使用ScheduledExecutorService而不是计时器。另外,我会使用ExecutorService让一个线程负责移动僵尸。 例如:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class ZombiesVsPlantsExample {

private static volatile Zombie[][] map = new Zombie[100][100]; // volatile not needed if you synchronize all access to the map. not only writes

    public static void main(String[] args) {
        ZombiesVsPlantsExample zombiesVsPlantsExample = new ZombiesVsPlantsExample();
        zombiesVsPlantsExample.doTheWork();
    }

    private void doTheWork() {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);  // this thread pool will be used to create zombies

        ExecutorService executorService = Executors.newFixedThreadPool(1);  // this thread pool will be used to move zombies



        ZombieCreator zombieCreator = new ZombieCreator(map);
        scheduler.scheduleAtFixedRate(zombieCreator, 2, 8, TimeUnit.SECONDS); // one new zombie every 8 seconds

        executorService.submit(new ZombiesPositionProcessor(map));

    }
}

class Zombie {

}

class ZombieCreator implements Runnable {

    Zombie[][] map;

    public ZombieCreator(Zombie[][] map) {
        this.map = map;
    }

    @Override
    public void run() {
        Zombie zombie = new Zombie();
        synchronized(map){
            map[1][2] = zombie;  // put new zombie in some random location in map
        }
        System.out.println("new zombie was created");
    }
}

class ZombiesPositionProcessor implements Runnable {

    Zombie[][] map;

    public ZombiesPositionProcessor(Zombie[][] map) {
        this.map = map;
    }

    @Override
    public void run() {
        while (true) {
            // iterate map and move zombies one by one
            System.out.println("moving one zombie");
        }
    }
}

这是一个设计问题。应该有什么东西来管理整个游戏吗?对象是否应该自行管理


就我个人而言,我认为你的想法是一个很好的方法自我管理的对象可以帮助减少游戏发展的复杂性。

这是一个设计问题。应该有什么东西来管理整个游戏吗?对象是否应该自行管理


就我个人而言,我认为你的想法是一种很好的方法自我管理的对象可以帮助减少游戏的复杂性。

这就做到了。谢谢这就成功了。谢谢