if语句将所有语句分组为一个if语句

时间:2022-09-19 07:35:36

how do you make an if statement that groups all the statements into one if statement at the moment I've got 20 or something if statements which isn't very efficient so i was wondering if there was a way to group the sprites so that if any of my sprites go off the top of the screen they reappear on the bottom and vice-versa.

你如何让一个if语句组的所有声明成一个if语句此刻我有20之类的if语句不是很有效,所以我想知道如果有一群精灵的方法,如果我的任何精灵离开屏幕的顶端,他们出现在底部,反之亦然。

At the moment I've got this; yellow1,2,3... being the sprite

现在我得到了这个;yellow1、2、3……是雪碧

if (yellow1.position.y-33>=320) {
  yellow1.position = ccp(yellow1.position.x,33);
}
if (yellow1.position.y+33<=0) {
  yellow1.position = ccp(yellow1.position.x,287);
}
if (yellow2.position.y-33>=320) {
  yellow2.position = ccp(yellow2.position.x,33);
}
if (yellow2.position.y+33<=0) {
  yellow2.position = ccp(yellow2.position.x,287);
}
if (yellow3.position.y-33>=320) {
  yellow3.position = ccp(yellow3.position.x,33);
}
if (yellow3.position.y+33<=0) {
  yellow3.position = ccp(yellow3.position.x,287);
}
if (yellow4.position.y-33>=320) {
  yellow4.position = ccp(yellow4.position.x,33);
}
if (yellow4.position.y+33<=0) {
  yellow4.position = ccp(yellow4.position.x,287);
}
if (yellow5.position.y-33>=320) {
  yellow5.position = ccp(yellow5.position.x,33);
}
if (yellow5.position.y+33<=0) {
  yellow5.position = ccp(yellow5.position.x,287);
}
if (yellow6.position.y-33>=320) {
  yellow6.position = ccp(yellow6.position.x,33);
}
if (yellow6.position.y+33<=0) {
  yellow6.position = ccp(yellow6.position.x,287);
}
if (yellow7.position.y-33>=320) {
  yellow7.position = ccp(yellow7.position.x,33);
}
if (yellow7.position.y+33<=0) {
  yellow7.position = ccp(yellow7.position.x,287);
}
if (yellow8.position.y-33>=320) {
  yellow8.position = ccp(yellow8.position.x,33);
}
if (yellow8.position.y+33<=0) {
  yellow8.position = ccp(yellow8.position.x,287);
}

here is my loop so far:

下面是我到目前为止的循环:

for (int i=0;i<16 ; i++) {
    if (((CCSprite *)[c1array objectAtIndex:i]).position.y-((CCSprite *)[c1array objectAtIndex:i]).contentSize.height>320) {
        ((CCSprite *)[c1array objectAtIndex:i]).position = ccp(((CCSprite *)[c1array objectAtIndex:i]).position.x,37);
    }
    if (((CCSprite *)[c1array objectAtIndex:i]).position.y+((CCSprite *)[c1array objectAtIndex:i]).contentSize.height<0) {
        ((CCSprite *)[c1array objectAtIndex:i]).position = ccp(((CCSprite *)[c1array objectAtIndex:i]).position.x,253);
    }
}

2 个解决方案

#1


4  

What you want is a loop. But first, you need to structure your data into a sequential data structure, such as an array. Then you will loop over each element in the array.

你想要的是一个循环。但是首先,您需要将数据结构为连续的数据结构,例如数组。然后对数组中的每个元素进行循环。

I am not familiar with iPhone programming, but here's the general psuedocode:

我不熟悉iPhone的编程,但是这里有一个通用的psuedocode:

// Create the list
const int SPRITE_COUNT = 8;
Sprite[] sprites = new Sprite[SPRITE_COUNT];
for(int i = 0; i < SPRITE_COUNT; ++i)
    sprites[i] = new Sprite();

// Later, check for sprites outside
for(int i = 0; i < SPRITE_COUNT; ++i)
{
    if ((sprites[i].position.y + 33) <= 0)
        ccp(sprite.position.x, 287);
    if ((sprites[i].position.y - 33) >= 320)
        ccp(sprite.position.x, 33);
}

#2


0  

You could put your sprites in an arraylist, and loop through the list. This would mean you are running the same amount of code, but letting the runtime do what its good at, and your code will be much tidier.

您可以将您的精灵放在arraylist中,并对列表进行循环。这意味着您正在运行相同数量的代码,但是让运行时做它擅长的事情,您的代码将更加整洁。

#1


4  

What you want is a loop. But first, you need to structure your data into a sequential data structure, such as an array. Then you will loop over each element in the array.

你想要的是一个循环。但是首先,您需要将数据结构为连续的数据结构,例如数组。然后对数组中的每个元素进行循环。

I am not familiar with iPhone programming, but here's the general psuedocode:

我不熟悉iPhone的编程,但是这里有一个通用的psuedocode:

// Create the list
const int SPRITE_COUNT = 8;
Sprite[] sprites = new Sprite[SPRITE_COUNT];
for(int i = 0; i < SPRITE_COUNT; ++i)
    sprites[i] = new Sprite();

// Later, check for sprites outside
for(int i = 0; i < SPRITE_COUNT; ++i)
{
    if ((sprites[i].position.y + 33) <= 0)
        ccp(sprite.position.x, 287);
    if ((sprites[i].position.y - 33) >= 320)
        ccp(sprite.position.x, 33);
}

#2


0  

You could put your sprites in an arraylist, and loop through the list. This would mean you are running the same amount of code, but letting the runtime do what its good at, and your code will be much tidier.

您可以将您的精灵放在arraylist中,并对列表进行循环。这意味着您正在运行相同数量的代码,但是让运行时做它擅长的事情,您的代码将更加整洁。