if 语句将所有语句分组为一个 if 语句
你如何制作一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将精灵放入数组列表中,然后循环遍历该列表。这意味着您正在运行相同数量的代码,但让运行时做它擅长的事情,您的代码将会更加整洁。
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.
你想要的是一个循环。但首先,您需要将数据构建为顺序数据结构,例如数组。然后您将循环遍历数组中的每个元素。
我不熟悉 iPhone 编程,但这里是一般的伪代码:
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: