2016-12-16 58 views
-1

好吧,我正在製作一款遊戲,讓它下雨。我用一個數組創建了這些狗,並用一個新線程將它們添加到了我的主類中,除了這些狗都沒有移動,我不知道爲什麼。任何人都可以幫助我發現我的錯誤? (注:這不是功課,我與我的空閒時間做這個)Java - 對象陣列不會移動

這裏有點我的代碼: 主類:

// 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"); 
    } 

} 

狗類:

// 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); 
} 
+0

剛剛通過了,沒有真正的分析快看......但是'-size/2'好像它可能是一個問題。你的意思是'--size/2'還是'(-1 * size)/ 2'?和lol @'doggo' –

+0

我認爲問題在於你在非UI線程上運行UI代碼,不是嗎? – Sweeper

+0

這不是你之後的問題,但我不明白'doggo'只有4個元素時,如何訪問'doggo [4]'沒有得到IndexOutOfBoundsException。 –

回答