如果语句只执行“一”,如果

时间:2015-03-25 04:42:22

标签: java

private void updateGame() {
    // Is the game won (or lost)?
    // Put here code to end the game (= no more aliens)
    this.window.suspendRepaints(); // to speed up the drawing
    // Update the ArrayList (remove the dead aliens)
    Iterator<Alien> it = aliens.iterator();
    while (it.hasNext()) {
        Alien a = it.next();
        if (a.isDead()) {
            it.remove();
        }
    }
    // Level 1



    // Move the aliens
    for (Alien a : aliens) {
        a.move();
    }

    // Move the space ship
    this.spaceShip.move();

    // Display it all
    this.window.resumeRepaints();


if (aliens.isEmpty() && counter==0) {
    counter++;
    this.aliens = new ArrayList<Alien>();
    System.out.println(counter);
    int z1 = 3 * SpaceShip.WIDTH;
    int x1 = 3 * SpaceShip.WIDTH;
    int y1 = 5 * Alien.RADIUS;


    for (int i = 0; i < 15; i++) {
        if (i > 12){
            this.aliens.add(new Alien(this.window, new Point (z1,y1+20)));
            x1+= 5* Alien.RADIUS;
        }else{
            this.aliens.add(new Alien(this.window,new Point (x1,y1)));
            x1+= 5* Alien.RADIUS;

        }
    }
}


    else if(aliens.isEmpty() && counter==1) {
        counter++;
    this.aliens = new ArrayList<Alien>();
    System.out.println(counter);
    int z2 = 3 * SpaceShip.WIDTH;
    int x2 = 3 * SpaceShip.WIDTH;
    int y2 = 5 * Alien.RADIUS;

    for (int i = 0; i < 18; i++) {
        if (i > 12){
            this.aliens.add(new Alien(this.window, new Point (z2,y2+20)));
            x2+= 5* Alien.RADIUS;
        }else{
            this.aliens.add(new Alien(this.window,new Point (x2,y2)));
            x2+= 5* Alien.RADIUS;

        }
    }
}

    else if (aliens.isEmpty() && counter==2) {
    counter++;
    this.aliens = new ArrayList<Alien>();
    System.out.println(counter);
    int z3 = 3 * SpaceShip.WIDTH;
    int x3 = 3 * SpaceShip.WIDTH;
    int y3 = 5 * Alien.RADIUS;
    for (int i = 0; i < 24; i++) {
        if (i > 12){
            this.aliens.add(new Alien(this.window, new Point (z3,y3+20)));
            x3+= 5* Alien.RADIUS;
        }else{
            this.aliens.add(new Alien(this.window,new Point (x3,y3)));
            x3+= 5* Alien.RADIUS;

        }
    }
}
}

我只是不明白为什么它除了第一个if语句之外没有使用任何东西。我的数量正在上升(我可以通过将其打印到控制台来判断)。但由于某种原因,它不会执行任何其他if语句。计数器在公共类SpaceInvader中定义,计数开头为0。问题是因为updateGame方法是一个void方法吗?我不相信,因为它在那个方法之外更新我的计数器,但我可能是错的?提前,我很抱歉,我是编程新手。

1 个答案:

答案 0 :(得分:0)

我没有遗憾地使用调试器。我发现我正在为所有的ArrayLists使用相同的x变量,所以每次都弹出相同的ArrayList。我真的不明白它是如何工作的,但我通过正确地命名变量来修复它,我只是忘了这样做。

this.aliens.add(new Alien(this.window, new Point (z2,y2+20)));
        x2+= 5* Alien.RADIUS;

应该是

this.aliens.add(new Alien(this.window, new Point (z2,y2+20)));
        z2+= 5* Alien.RADIUS;

不同之处在于我的变量z2 vs x2,我在每个新的ArrayList添加中使用x2,这搞乱了ArrayList。现在都是桃子!