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

发布于 2024-10-14 09:07:46 字数 2181 浏览 3 评论 0原文

你如何制作一个 if 语句,将所有语句分组为一个 if 语句,目前我有 20 个或其他 if 语句,这不是很有效,所以我想知道是否有一种方法可以将精灵分组,以便如果我的任何精灵都会离开屏幕顶部,然后重新出现在底部,反之亦然。

目前我已经得到了这个; 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);
}

,这是我到目前为止的循环:

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

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.

At the moment I've got this; yellow1,2,3... being the 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 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);
    }
}

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

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

发布评论

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

评论(2

烧了回忆取暖 2024-10-21 09:07:50

您可以将精灵放入数组列表中,然后循环遍历该列表。这意味着您正在运行相同数量的代码,但让运行时做它擅长的事情,您的代码将会更加整洁。

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.

通知家属抬走 2024-10-21 09:07:49

你想要的是一个循环。但首先,您需要将数据构建为顺序数据结构,例如数组。然后您将循环遍历数组中的每个元素。

我不熟悉 iPhone 编程,但这里是一般的伪代码:

// 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);
}

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:

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