简体   繁体   English

连续时while循环的冲突(Java)

[英]Collisions of while loops when in a row (Java)

I build a game that includes moving between worlds. 我制作了一个游戏,其中包括在世界之间移动。 The main class of the program suppose to start by presenting the first world (world1 which is an instance of the firstWorldClass). 该程序的主类假定首先显示第一个世界(world1,它是firstWorldClass的一个实例)。 It should stay in this world until a variable within the class world1 is changing (checked by world1.getMoveWorldIndicator()). 它应该留在这个世界中,直到类world1中的变量发生更改(由world1.getMoveWorldIndicator()检查)。 Then, it should move to the next world- remove world1, build world2 and add it to the graphic view. 然后,它应该移动到下一个世界-删除world1,构建world2并将其添加到图形视图。 Same from world 2 to 3. It works fine when I do it between 2 worlds (delete all the code after add(world2)) or if I leave only the transition between the second and the third worlds. 从世界2到世界3相同。当我在2个世界之间进行操作(删除add(world2)之后的所有代码)或仅保留第二个世界与第三个世界之间的过渡时,它工作正常。 When I put both in a row it stops working (when adding the second while loop). 当我将两者都放在一行中时,它将停止工作(添加第二个while循环时)。 My code: 我的代码:

public class GameManager extends Program implements GameContstants{

public void init() {
    world1=new firstWorldClass();
    add(world1);
    while(!world1.getMoveWorldIndicator()){
        pause(200);
        if(world1.getMoveWorldIndicator())break;
    }
    remove(world1);
    world1=null;
    world2=new secondWorldClass();
    add(world2);
    pause(200);
    while(! world2.getMoveWorldIndicator()){
        pause(200);
        if(world2.getMoveWorldIndicator()) break;
    }
    remove(world2);
    world2=null;
    world3=new thirdWorldClass();
    add(world3);
}   

private firstWorldClass world1;
private secondWorldClass world2;
private thirdWorldClass world3;

} }

I suspect that there's some kind of interaction between the while loops or a memory problem but am open to hear any idea/different way to do it. 我怀疑while循环或内存问题之间存在某种相互作用,但是很容易听到任何想法/不同的方式来做。

Thanks! 谢谢!

I think there are lots of factors coming into play here: 我认为这里有很多因素在起作用:

  • Clean up your code. 清理您的代码。 Keeping a few coding rules is important for bug fixing! 保持一些编码规则对于修复错误很重要!
  • Don't use repetitive code over and over again. 不要重复使用重复的代码。 Extract to method or Class 提取到方法或类
  • Keep to class naming conventions => Upper camel case for classes 遵守班级命名约定=>班级的驼峰式大写
  • Don't set variables to null. 不要将变量设置为null。 Usually compilers will do that for you while optimizing. 通常,编译器会在优化时为您执行此操作。 If you still feel like manually freeing up space or variable names, you should use scope brackets {} around the code instead! 如果您仍然想手动释放空间或变量名,则应在代码周围使用范围括号{}!
  • Breaking down your code, we see lots of redundancies, like "getMoveWorldIndicator()" checks twice per loop, without any different effect from using it only once 分解您的代码,我们看到很多冗余,例如“ getMoveWorldIndicator()”每个循环检查两次,与仅使用一次没有任何不同的影响

I have provided a cleaner - but different - version here, to show you. 我在这里提供了一个清洁器-但有所不同-版本给您看。

But for actually helping you with your code, we really DO NEED more of your code to gain some insight to the problem. 但是,为了真正帮助您使用代码,我们确实需要更多代码来深入了解问题。

public class GameManager /* extends ... */{

    static class WorldBase {
        public boolean getMoveWorldIndicator() {
            return false;
        }
    }
    static class FirstWorld extends WorldBase {}
    static class SecondWorld extends WorldBase {}
    static class ThirdWorld extends WorldBase {}

    public void init() {
        playWorld(new FirstWorld());
        playWorld(new SecondWorld());
        playWorld(new ThirdWorld());
    }

    private void playWorld(final WorldBase pWorld) {
        add(pWorld);
        while (!pWorld.getMoveWorldIndicator()) {
            pause(200);
        }
        remove(pWorld);
    }

    private void remove(final WorldBase pWorld1) {}
    private void pause(final int pI) {}
    private void add(final WorldBase pWorld1) {}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM