gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:42:16 27 4
gpt4 key购买 nike

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

目前我已经得到了这个; 黄色1,2,3...是 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);
}

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

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

最佳答案

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

我不熟悉 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);
}

关于iphone - if 语句将所有语句分组为一个 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4787229/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com