Java—赢得的对象数组';别动

Java—赢得的对象数组';别动,java,object,multidimensional-array,2d-games,Java,Object,Multidimensional Array,2d Games,好吧,我正在做一个游戏,里面下着狗的雨。我用一个数组创建了这些狗,并用一个新的线程将它们添加到我的主类中,但是除了狗之外,所有的狗都没有移动,我不知道为什么。有人能帮我发现错误吗?(注意:这不是家庭作业,我是利用空闲时间做的) 下面是我的一些代码: 主要类别: // instance variables private double width, height; Dogs[] doggo = new Dogs[4]; // an array of dogs int count = 3; // c

好吧,我正在做一个游戏,里面下着狗的雨。我用一个数组创建了这些狗,并用一个新的线程将它们添加到我的主类中,但是除了狗之外,所有的狗都没有移动,我不知道为什么。有人能帮我发现错误吗?(注意:这不是家庭作业,我是利用空闲时间做的)

下面是我的一些代码: 主要类别:

// instance variables
private double width, height;
Dogs[] doggo = new Dogs[4]; // an array of dogs
int count = 3; // counts the amount of lives remaining
int counter = 0; // counts how many dogs are saved

public void drawGraphics(){
    // draw the dogs
    for (int i = 0; i < 5; i++) {
        double x = rand.nextDouble(SIZE, (getWidth()-10)-SIZE);

        doggo[i] = new Dogs(SIZE, speed, this);
        // add dog to top of the window
        add(doggo[i], x, 0);
        new Thread(doggo[i]).start(); // animate the dogs
        //System.out.println("try");
    }

    for (int i = 0; i < 5; i++) {
        double x = rand.nextDouble(10, (getWidth()-10)-SIZE);

        doggo[i] = new Dogs(SIZE, speed*2, this);
        // add dog to top of the window
        add(doggo[i], x, 0);
        new Thread(doggo[i]).start(); // animate the dogs
        //System.out.println("try");
    }

}
编辑:

Dogs类需要实现Runnable,以便在启动线程时可以调用run()方法

您可以在此处查看oracle文档以获取示例:

只是快速浏览了一下,没有进行真正的分析……但是
-size/2
似乎有问题。你是说
--size/2
还是
(-1*size)/2
?lol@
doggo
我认为问题在于你在非UI线程上运行UI代码,不是吗?这不是你要解决的问题,但我不明白当
doggo
只有4个元素时,你怎么没有从访问
doggo[4]
中获得IndexOutOfBoundsException。
// constants
private static final double DELAY = 50;

// instance variables
private double size, speed;
DoggoRescue game; // to recognize use of dogs in DoggoRescue game
GImage dog;    

public Dogs(double size, double speed, DoggoRescue game){
    // save the parameters to the instance variables
    this.game = game;
    this.size = size;
    this.speed = speed;

    // draw an image of the dog
    dog = new GImage("Doggo.png");
    dog.setSize(size, size);
    add(dog, -size/2, -size/2);
}

// animate the dog
public void run(){
    oneTimeStep();
    pause(DELAY);
}

// called by run(), move the dog
private void oneTimeStep(){
    // move dog
    move(0, speed);
    //System.out.println("try");
    pause(DELAY);
    // call checkCollision of the main class
    game.checkCollision(this);
}