0

我正在开发一款游戏,我正在尝试优化一些东西。我做了一个 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;
            }
        }
    }

当我重新编写代码时,某些东西显然没有翻译过来,因为当我尝试在游戏冲突中切换它时,测试停止工作。不能为我的生活看到什么可能是错的,有什么想法吗?

4

2 回答 2

0

您正在使用&运算符,请将其更改为&&

你的if情况应该如下:

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))) 
于 2012-10-29T05:33:38.567 回答
0
  1. 使用逻辑 &&,而不是按位 & 运算符。

  2. 考虑

    boolean[] pbGoodList = { pb1Good, pb2Good, pb3Good, pb4Good }
    

    pbGoodList[i] = true;
    

    你没有改变pb1Good, pb2Good, pb3Good, 或者pb4Good像你在显示的原始代码中所做的那样。当您pbGoodList使用这些值初始化数组时,pbGoodList会保留这些值的唯一不同实例。

    您可以在循环之后将数组中的值复制到各个布尔变量中:

    pb1Good = pbGoodList[0];
    ...
    
于 2012-10-29T05:40:56.057 回答