Objective c 很多if语句需要帮助进行某种循环
我需要一个更快的方法来写这个Objective c 很多if语句需要帮助进行某种循环,objective-c,xcode,if-statement,cocos2d-iphone,touchesmoved,Objective C,Xcode,If Statement,Cocos2d Iphone,Touchesmoved,我需要一个更快的方法来写这个 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
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处的对象是一个等于点1(33,33)的精灵。精灵将在cctouchmoved中调用,因此我可以在该点移动精灵,但数组是这样的,因此我可以移动一列精灵,因为我知道它们都处于正确的位置。现在我的问题是,所有这些都是很长的,我必须做一个循环或什么的 代替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
}
您可以将指向16个精灵的指针存储在一个数组中,还可以有一个需要对其进行检查的点数组。然后使用嵌套的for循环进行检查。哪个精灵击中哪个点将由这些循环的当前计数器指示 在这个示例中我没有使用点类型(例如CGPoint),而是使用一个单独的x点和y点数组。因此,您感兴趣的任何一点都应该存储在两个数组的同一元素中。例如,如果第7点是(55,97),那么
myXPoints[7]=55代码>和myYPoints[7]=97代码>
该示例在16个sprite指针的数组中循环。在每个循环中,它将循环通过成对的点阵列。当有命中时,循环计数器指示命中发生的位置
免责声明:这只显示了一种可能的技术,可能需要调整以适应您的类型和数据结构等
#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
}
}
}
#定义NUMSPRITES 16
#定义NUMPOINTS 16
//这些数组将包含要比较的数据
int myXPoints[NUMPOINTS];
int myYPoints[NUMPOINTS];
雪碧*mySpritePointers[NUMSPRITES];
//用指向我的精灵的指针填充精灵数组
myPritePointers[0]=&myPriteArray[0]。精灵1;
myPritePointers[1]=&myPriteArray[0]。精灵2;
... 等
mySpritePointers[15]=&mySpriteArray[3]。Sprite4;
//填充点阵列
myXPoints[0]=33;
... 等
myXPoints[15]=97;
myYPoints[0]=33;
... 等
myYPoints[15]=97;
//现在检查每个精灵
对于(int-nSprite=0;nSpriteposition.x==myXPoints[nPoint]&&mypritepointers[nSprite]->position.y==myYPoints[nPoint])
{
//这里有一些代码将mySpritePointers[nSprite]添加到索引nPoint处的数组对象中
}
}
}
祝你好运 @Dave,我对“&mySpriteArray”有点困惑,我该怎么说呢,nsarray?“mySpritePointers[nSprite]->position.x”是指在if语句中由“mySpriteArray”表示的“->position.x”,我指的是精灵当前居住的任何对象。您有名为sprite1、sprite2、sprite3和sprite4的精灵。它们位于什么对象中,该对象的实例称为什么?我“猜测”它被称为“mySpriteArray”。我想做的就是找到一个指向那个精灵的指针。不管你喜欢怎么做。对于'->position.x',它只是意味着它使用了指向精灵的指针,而不是精灵本身。@Dave,精灵指针'>position.x'不起作用说'stuct CCSprite'没有成员名称'position'您的精灵1、精灵2等是什么类型的?mySpritePointers数组需要与sprite1的类型相同,但是当我在循环中将sprite[x]声明为ccsprite*sprite[x]时,需要指向该类型的指针;应用程序在调试时崩溃。