0

我需要一种更快的方法来写这个。

if(sprite1.position.x==33 && sprite1.position.y==33){
// some code goes here to add sprite1 to an array object at index 0
}
if(sprite2.position.x==33 && sprite2.position.y==33){
// some code goes here to add sprite2 to an array object at index 0
}
if(sprite3.position.x==33 && sprite3.position.y==33){
// some code goes here to add sprite3 to an array object a index 0
}
if(sprite4.position.x==33 && sprite4.position.y==33){
// some code goes here to add sprite4 to an array object at index 0
} ....e.t.c

if(sprite1.position.x==33 && sprite1.position.y==97){
// some code goes here to add sprite1 to an array object at index 1
}
if(sprite2.position.x==33 && sprite2.position.y==97){
// some code goes here to add sprite2 to an array object at index 1
}

我有 4 个数组,每个数组包含 4 个精灵。

我有 16 个点和 16 个精灵。每个精灵都有一个随机点,所以我必须检查每个精灵是否等于该点,并且每个精灵只有一个点。然后将其添加到数组中。我从索引 0 处的对象的数组中调用该对象,因为我确定索引 0 处的对象是一个精灵,它等于 point1,即 (33,33)。sprite 将在 cctouchmoved 中调用,因此我可以在该点移动 sprite,但数组是这样我可以移动一列 sprite,因为我知道它们都在正确的位置。现在我的问题是输入所有这些很长我必须做一个循环或其他东西。

4

2 回答 2

0

您也许可以将指向您的 16 个精灵的指针存储在一个数组中,并且还有一个您需要检查它们的点数组。然后有一个嵌套的 for 循环进行检查。哪个精灵命中哪个点,然后由这些循环的当前计数器指示

在此示例中,我没有使用点类型(例如 CGPoint),而是使用了一个单独的 x 点和 y 点数组。所以你感兴趣的任何一点都应该存储在两个数组的同一个元素中。例如,如果您的第 7 点是 (55, 97),那么myXPoints[7] = 55;myYPoints[7] = 97;

该示例循环遍历您的 16 个精灵指针数组。在每个循环中,它将遍历成对的点数组。当您命中时,循环计数器会指示命中发生的位置。

免责声明:这仅显示了一种可能的技术,并且可能需要适应您的类型和数据结构等。

    #define NUMSPRITES 16
    #define NUMPOINTS 16

    // These arrays are to contain the data that will be compared

    int myXPoints [NUMPOINTS];
    int myYPoints [NUMPOINTS];
    Sprite *mySpritePointers [NUMSPRITES];

    // Populate the sprite array with pointers to my sprites
    mySpritePointers [0] = &mySpriteArray[0].Sprite1;
    mySpritePointers [1] = &mySpriteArray[0].Sprite2;
    ... etc ...
    mySpritePointers [15] = &mySpriteArray[3].Sprite4;

    // Populate the points array
    myXPoints [0] = 33;
    ... etc ...
    myXPoints [15] = 97;

    myYPoints [0] = 33;
    ... etc ...
    myYPoints [15] = 97;

    // Now check each sprite
    for (int nSprite = 0;  nSprite < NUMSPRITES;  nSprite++)
    {
        // And check each point
        for (int nPoint = 0;  nPoint < NUMPOINTS;  nPoint++)
        {
            // check this sprite against the x and the y points for this point
            if(mySpritePointers [nSprite]->position.x == myXPoints [nPoint] && mySpritePointers [nSprite]->position.y == myYPoints [nPoint])
            {
                // some code goes here to add mySpritePointers [nSprite] to an array object at index nPoint
            } 
        }
    }

祝你好运!

于 2011-02-21T12:03:55.707 回答
0

除了 sprite1、sprite2、sprite3 等,你能没有一个数组并在循环中使用 sprite[x] 吗?

if(sprite[x].position.x==33 && sprite[x].position.y==33){
// some code goes here
}

if(sprite[x].position.x==33 && sprite[x].position.y==97){
// some code goes here
}
于 2011-02-21T11:32:17.637 回答