开发者

Xcode CGRect Collision With multiple CGRects?

I have created a game where I have a floor made of multiple images varying in heights.. (So the floor isn't flat) Rather then opening a million if statements:

if (CGRectIntersectsRect(ball1.frame, floor1.frame)) { }

if (CGRectIntersectsRect(ball1.frame, floor2.frame)) { }

if (CGRectIntersectsRect(bal开发者_开发知识库l1.frame, floor3.frame)) { }

if (CGRectIntersectsRect(ball1.frame, floor4.frame)) { }

//and so on...

.

Is there a way I can check collision using 1 if statement? All of the UIImageView floor pieces are named, "floor1, floor2, floor3, floor4, and so on"

Thanks!

Side note: If I want to use the CGRectIntersect code and I rotate the image I'm checking the intersection for it seems to still only check the collision for the image frame, so wether I rotate it or not it collides in the same spot. Can I rotate the frame as well to fix this or do I need to use different code, if so what code?


While I don't quite understand your question, I think I can offer a simplification. Instead of naming the tiles floor1, floor2, ..., you should instead use an array. In this way, you could collapse the if statements to 3 statements:

for (int i = 0; i < num_tiles; i++) {
    if (CGRectIntersectsRect(ball1.frame, floor[i].frame)) {
         put code here
    }
}


Definitely change your naming scheme, item1, item2, item3 is never the right option where there's more than 2 or 3 items. If you have to, just say

NSMutableArray* floors = [NSMutableArray array];
[floors addObject:floor1];
[floors addObject:floor2];
[floors addObject:floor3];
... etc

Before the code that No One In Particular suggested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜