我的方法存在一个问题,我用它来突出怪物(我只是简单的精灵),但问题是,它并没有突出显示每个怪物,只有最后一个添加。
这是突出显示的方法:
public void highlightMonsters(){
while (mStack.size()!=0){
if (mStack.peek().toString().equals("1")) {
monsterTopLeft.setVisible(!visibility);
monsterTopLeftColored.setVisible(visibility);
monsterTopLeftColored.registerUpdateHandler(changeColorsMonster1);
mReverseStack.push(mStack.pop());
monsterTopLeft.registerUpdateHandler(cakaj);
}
else if(mStack.peek().toString().equals("2")) {
monsterTopRight.setVisible(!visibility);
monsterTopRightColored.setVisible(visibility);
monsterTopRightColored.registerUpdateHandler(changeColorsMonster2);
mReverseStack.push(mStack.pop());
monsterTopRight.registerUpdateHandler(cakaj);
}
else if(mStack.peek().toString().equals("3")) {
monsterBottomLeft.setVisible(!visibility);
monsterBottomLeftColored.setVisible(visibility);
monsterBottomLeftColored.registerUpdateHandler(changeColorsMonster3);
mReverseStack.push(mStack.pop());
monsterBottomLeft.registerUpdateHandler(cakaj);
}
else if (mStack.peek().toString().equals("4")){
monsterBottomRight.setVisible(!visibility);
monsterBottomRightColored.setVisible(visibility);
monsterBottomRightColored.registerUpdateHandler(changeColorsMonster4);
mReverseStack.push(mStack.pop());
monsterBottomRight.registerUpdateHandler(cakaj);
}
}
}
这是主循环
public void mainLoop() {
cislo = generateNumber();
testText.setText(String.valueOf(cislo));
mReverseStack.push(new Integer(cislo)); //debugging purposes
//cisla su v mReverseStack
reverseNumbersInStack2();
//prehodime do mStack
highlightMonsters();
//vysvietime a zaroven prehodime do mReverseStack
reverseNumbersInStack2();
//prehodime do mStack
}
答案 0 :(得分:0)
如果您希望while()
周期减少堆叠,请使用pop()
代替peek()
答案 1 :(得分:0)
我不确定您的代码是否适合您的目的,但如果您希望代码能够正常工作,那么您就不会偷看,而是会弹出。每次循环一次 - 如果你没有覆盖一个数字,那么在if语句中执行它可能会导致无限循环。如果代码的其他部分没有错误,那么这样的事情应该有效:
public void highlightMonsters(){
while (mStack.size()!=0){
String monsterString = mStack.pop().toString();
switch(monsterString)
case "1" : {
monsterTopLeft.setVisible(!visibility);
monsterTopLeftColored.setVisible(visibility);
monsterTopLeftColored.registerUpdateHandler(changeColorsMonster1);
monsterTopLeft.registerUpdateHandler(cakaj);
break;
}
case "2" : {
monsterTopRight.setVisible(!visibility);
monsterTopRightColored.setVisible(visibility);
monsterTopRightColored.registerUpdateHandler(changeColorsMonster2);
monsterTopRight.registerUpdateHandler(cakaj);
break;
}
case "3" : {
monsterBottomLeft.setVisible(!visibility);
monsterBottomLeftColored.setVisible(visibility);
monsterBottomLeftColored.registerUpdateHandler(changeColorsMonster3);
monsterBottomLeft.registerUpdateHandler(cakaj);
break;
}
case "4" : (mStack.peek().toString().equals("4")){
monsterBottomRight.setVisible(!visibility);
monsterBottomRightColored.setVisible(visibility);
monsterBottomRightColored.registerUpdateHandler(changeColorsMonster4);
monsterBottomRight.registerUpdateHandler(cakaj);
break;
}
default : break;
}
mReverseStack.push(monsterString);
}
}
但是,我真的建议不要使用这种代码。你应该尝试在堆栈而不是数字上推动你自己的怪物,并使changeColors-Stuff更加通用的参数。或者,至少将if或case语句中的一些代码放入单独的方法中。上面代码中的这种冗余不是很好的编码风格,你迟早会遇到问题。