产卵僵尸延迟?

问题描述 投票:-1回答:1

我试图在每个产卵之间产生第二次延迟产生僵尸,但没有运气,即使我的计时器也没有延迟,它似乎是循环但我不知道我做错了什么?

任何人都可以提供帮助将不胜感激<3

public class Zombie extends GameObject{

    Rectangle[] zombies = new Rectangle[60];

    Timer timer = new Timer();

    public Zombie(float x, float y, ID id, SpriteSheet ss) {
        super(x, y, id, ss);

        Delay();
    }

    public void Delay(){
        timer.schedule(task, 0, 1000);

    }

    TimerTask task = new TimerTask(){
        public void run(){

            for(int i = 0; i < zombies.length; i++){

                Random rand = new Random();
                int result = rand.nextInt(6);

                if(result == 0){
                    x = 1000;
                    y = 200;
                }

                if(result == 1){
                    x = 100;
                    y = 200;
                }

                if(result == 2){
                    x = 100;
                    y = 600;
                }

                if(result == 3){
                    x = 1000;
                    y = 600;
                }

                if(result == 4){
                    x = 100;
                    y = 900;
                }

                if(result == 5){
                    x = 1000;
                    y = 900;
                }

                zombies[i] = new Rectangle((int)x, (int)y, 32, 48);

                if(i >= zombies.length){
                    task.cancel();
                }

            }
        }
    };

    public void tick() {

    }

    public void render(Graphics g) {

        for(int i = 0; i < zombies.length; i++){
            g.setColor(Color.YELLOW);
            g.fillRect((int)zombies[i].x, (int)zombies[i].y, 32, 48);
        }
    }

    public Rectangle getBounds() {
        return new Rectangle((int)x, (int)y, 32, 48);
    }
}
java
1个回答
0
投票

请查看以下更改。我没有测试过这个,但我认为它会起作用。我添加了spawnedZombies计数器并删除了你的for循环。我还改变了停止计时器任务的条件。

我同意迈克尔的评论,这是令人困惑的,你在僵尸类中有60个僵尸但这是另一个讨论:)

  Rectangle[] zombies = new Rectangle[60];

        Timer timer = new Timer();

        int spawnedZombies = 0;

        public Zombie(float x, float y, ID id, SpriteSheet ss) {
            super(x, y, id, ss);

            Delay();
        }

        public void Delay(){
            timer.schedule(task, 0, 1000);

        }

        TimerTask task = new TimerTask()
        {
            public void run()
            {
                Random rand = new Random();
                int result = rand.nextInt(6);

                if(result == 0){
                    x = 1000;
                    y = 200;
                }

                if(result == 1){
                    x = 100;
                    y = 200;
                }

                if(result == 2){
                    x = 100;
                    y = 600;
                }

                if(result == 3){
                    x = 1000;
                    y = 600;
                }

                if(result == 4){
                    x = 100;
                    y = 900;
                }

                if(result == 5){
                    x = 1000;
                    y = 900;
                }
        spawnedZombies++;
        zombies[spawnedZombies - 1] = new Rectangle((int)x, (int)y, 32, 48);


                if(spawnedZombies == zombies.length){
                    task.cancel();
                }

            }
        };
© www.soinside.com 2019 - 2024. All rights reserved.