很多 if 语句,需要帮助进行某种循环

发布于 2024-10-17 00:02:37 字数 1128 浏览 2 评论 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)。该精灵将在 cctouchmoved 中调用,因此我可以在该点移动精灵,但数组是这样的,因此我可以移动一列精灵,因为我知道它们都位于正确的位置。现在我的问题是输入的所有内容都很长,我是否必须进行循环或其他操作。

I need a quicker way to write this.

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
}

I have 4 arrays that hold 4 sprites each.

I have 16 points and 16 sprites. Each sprite has been give a random point so I have to check every sprite if it is equal to the point and each sprite has only one point. It is then added to an array. I call the object from the array at object at index 0 as i know for sure that the object at index 0 is a sprite that is equal to point1 which is(33,33). The sprite will be called in cctouchmoved, so i can move the sprite at that point but the array is so i can move a column of sprites as i know that they are all in the right positions. Right now my problem is typing all of that is very long do i have to make a loop or something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-10-24 00:02:37

您也许可以将指向 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
            } 
        }
    }

祝您好运!

You could perhaps store pointers to your 16 sprites in an array, and also have an array of points that you need to check them against. Then have a nested for loop doing the checking. Which sprite hit which point will then be indicated by the current counter of those loops

In this example I have not used a point type (eg CGPoint), but a separate array of x points and y points. So any one point you are interested in should be stored in the same element of the two arrays. Eg if you 7th point is (55, 97), then myXPoints[7] = 55; and myYPoints[7] = 97;

The example loops through your array of 16 sprite pointers. Within each loop, it will loop through the paired array of points. When you have a hit, the loop counters indicate where the hit occurred.

Disclaimer: This only shows a possible technique, and will probably need adapting to accomodate your types and data structures etc.

    #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
            } 
        }
    }

Good luck!

濫情▎り 2024-10-24 00:02:37

您可以不使用数组并在循环中使用 sprite[x] 来代替 sprite1、sprite2、sprite3 等吗?

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
}

Instead of sprite1, sprite2, sprite3 etc, can you not have an array and use sprite[x] in a loop?

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文