为什么我的循环结束了?

时间:2015-02-20 20:46:37

标签: java loops libgdx

在我正在制作的游戏中,我有一个在我的libgdx屏幕的show方法中运行的循环。循环基本上说战斗正在进行。它是一个while循环,在布尔“iscombat = true”时运行。但是,在循环中我将iscombat设置为= false,并且它似乎并不重要。循环继续。我想也许我犯了一些错误,所以我输入一个递增的整数,并告诉循环结束时该整数达到30.循环继续。

然后我写了它来打印出整数的值,以防它没有递增。不,我看着它超过30并且循环仍未结束。所以然后我写了程序,当整数超过30时,字面上改变屏幕,并且它仍然只是继续运行循环,即使它打印出整数超过30。

以下是while循环的代码:

while(combatisgoing = true){                                         //begin the combat loop.  all of combat happens in this loop?
blah++;    
System.out.println(blah);
thisencounter.TurnCounter();                                        //we are in combat now, so start the turn counting 

if(blah > 30){((Game)Gdx.app.getApplicationListener()).setScreen(new Home());}
if(blah > 30){  combatisgoing = false; }

if(thisencounter.monster1turn=true){                                //monster1's turn goes in here
    thiscombat.getTarget(1);                                        //get enemy's target
    thiscombat.calculateVars(1);                                     //calculate all combat variables for this turn
    thiscombat.doAttack();
    System.out.println(Encounter.enemies.get(0).species+"attacked");
    System.out.println("Your spirits healths are:"+OwnedSpirits.myspirits.get(0).health+""+OwnedSpirits.myspirits.get(1).health+OwnedSpirits.myspirits.get(2).health);
thisencounter.monster1turn=false;
}    

}

代码多于此,但其他内容根本不相关。 如果你想看看转弯器看起来是什么样的,我不明白它为什么重要,但这里是

 public void TurnCounter(){
    x=0;
    while(x==0){
    monster1rate = monster1rate + monster1rate2;
    monster2rate = monster2rate + monster2rate2;
    monster3rate = monster3rate + monster3rate2;
    spirit1rate = spirit1rate + spirit1rate2;
    spirit2rate = spirit2rate + spirit2rate2;
    spirit3rate = spirit3rate + spirit3rate2;

if(monster1rate > 100000){

  monster1rate = monster1rate - 100000;
  monster1turn = true;
  x=1;
}
else if(monster2rate > 100000){

  monster1rate = monster1rate - 100000;
  monster2turn = true;
  x=1;
}
else if(monster3rate > 100000){

  monster1rate = monster1rate - 100000;
  monster3turn = true;
  x=1;
}
else if(spirit1rate > 100000){

  spirit1rate = spirit1rate - 100000;
  spirit1turn = true;
  x=1;
}
if(spirit2rate > 100000){

  spirit2rate = spirit2rate - 100000;
  spirit2turn = true;
  x=1;
}
if(spirit3rate > 100000){

  spirit3rate = spirit3rate - 100000;
  spirit3turn = true;
  x=1;
}



}
}

这只是一种确定在我的游戏中轮到谁的方式。

游戏的输出刚刚射出,它一直向上递增,当我进站时它看起来像这样:

"Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
793
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
794
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
795
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
796
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
797
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
798"

这正是我期望我的输出看起来不像是循环没有在blah>停止时30。

请帮助lol

2 个答案:

答案 0 :(得分:4)

此分配始终为true

while (combatisgoing = true) {      

将其更改为

while (combatisgoing) {      

答案 1 :(得分:0)

如果while(combatisgoing) 对你来说很困惑...... 试试while(combatisgoing==true)

一个等于意味着您正在设置一个值。在这种情况下,你正在使战斗永远是真实的。两个等于是指示某事物是否相等的运算符。