If, which groups all statements into a single if statement

how do you do an if statement that groups all the statements into a single if statement at the moment when I have 20 or something, if the statements are not very efficient, so I was wondering if there is a way to group sprites so if any of my sprites will leave the top of the screen, they will appear again below and vice versa.

At the moment I have it; yellow1,2,3 ... being a sprite

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:

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);
    }
}
+3
source share
2 answers

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 iterate over each element in the array.

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);
}
+4

arraylist . , , , , .

0

Source: https://habr.com/ru/post/1787553/