我正在开发一款游戏,我正在尝试优化一些东西。我做了一个 for 循环来缩短以下代码:
// if near pb1
if (pbMoveCheck_playerNearpb(pb1, pb2, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb1, pb2, x, y)) {
pb1Good = true;
pGood = true;
}
// if near pb1
if (pbMoveCheck_playerNearpb(pb1, pb3, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb1, pb3, x, y)) {
pb1Good = true;
pGood = true;
}
// if near pb1
if (pbMoveCheck_playerNearpb(pb1, pb4, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb1, pb4, x, y)) {
pb1Good = true;
pGood = true;
}
// if near pb2
if (pbMoveCheck_playerNearpb(pb2, pb1, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb2, pb1, x, y)) {
pb2Good = true;
pGood = true;
}
// if near pb2
if (pbMoveCheck_playerNearpb(pb2, pb3, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb2, pb3, x, y)) {
pb2Good = true;
pGood = true;
}
// if near pb2
if (pbMoveCheck_playerNearpb(pb2, pb4, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb2, pb4, x, y)) {
pb2Good = true;
pGood = true;
}
// if near pb3
if (pbMoveCheck_playerNearpb(pb3, pb1, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb3, pb1, x, y)) {
pb3Good = true;
pGood = true;
}
// if near pb3
if (pbMoveCheck_playerNearpb(pb3, pb2, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb3, pb2, x, y)) {
pb3Good = true;
pGood = true;
}
// if near pb3
if (pbMoveCheck_playerNearpb(pb3, pb4, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb3, pb4, x, y)) {
pb3Good = true;
pGood = true;
}
// if near pb4
if (pbMoveCheck_playerNearpb(pb4, pb1, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb4, pb1, x, y)) {
pb4Good = true;
pGood = true;
}
// if near pb4
if (pbMoveCheck_playerNearpb(pb4, pb2, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb4, pb2, x, y)) {
pb4Good = true;
pGood = true;
}
// if near pb4
if (pbMoveCheck_playerNearpb(pb4, pb3, b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(pb4, pb3, x, y)) {
pb4Good = true;
pGood = true;
}
所以我把它砍成这样:
// Make lists of stuff
pushBlock1[] listPushBlocksA = { pb1, pb2, pb3, pb4 };
boolean[] pbGoodList = { pb1Good, pb2Good, pb3Good, pb4Good };
// if near pb loop
for (int i = 0; i < listPushBlocksA.length; i++) {
for (int j = 0; j < listPushBlocksA.length; j++) {
if (i != j & pbMoveCheck_playerNearpb(listPushBlocksA[i],
listPushBlocksA[j], b1, b2, b3, b4, m, p, x, y)
& pbMoveCheck_doublepb(listPushBlocksA[i],
listPushBlocksA[j], x, y)) {
pbGoodList[i] = true;
pGood = true;
}
}
}
当我重新编写代码时,某些东西显然没有翻译过来,因为当我尝试在游戏冲突中切换它时,测试停止工作。不能为我的生活看到什么可能是错的,有什么想法吗?