很多 if 语句,需要帮助进行某种循环
我需要一种更快的方法来写这个。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也许可以将指向 16 个精灵的指针存储在一个数组中,并且还有一个需要检查它们的点数组。然后使用嵌套的 for 循环进行检查。哪个精灵击中哪个点将由这些循环的当前计数器指示。
在这个示例中,我没有使用点类型(例如 CGPoint),而是使用 x 点和 y 点的单独数组。因此,您感兴趣的任何一点都应该存储在两个数组的同一元素中。例如,如果您的第 7 个点是 (55, 97),则
myXPoints[7] = 55;
和myYPoints[7] = 97;
该示例循环遍历您的 16 数组精灵指针。在每个循环中,它将循环遍历成对的点数组。当您点击时,循环计数器会指示点击发生的位置。
免责声明:这仅显示了一种可能的技术,并且可能需要进行调整以适应您的类型和数据结构等。
祝您好运!
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;
andmyYPoints[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.
Good luck!
您可以不使用数组并在循环中使用 sprite[x] 来代替 sprite1、sprite2、sprite3 等吗?
Instead of sprite1, sprite2, sprite3 etc, can you not have an array and use sprite[x] in a loop?