for (platforms in platforms) { // 語法錯誤,且 platforms 既是數組又是循環變量
if (CGRectIntersectsRect(ball.frame, platforms.frame)) { ... }
}
NSMutableArray *platforms = [NSMutableArray array];
[platforms addObject:platform1];
[platforms addObject:platform2];
// ... 添加更多
if ([platforms containsObject:platform1]) {
NSLog(@"platform1 存在於數組中");
} else {
NSLog(@"不存在");
}
若需檢測小球與所有平台碰撞:
for (UIView *platform in platforms) {
if (CGRectIntersectsRect(ball.frame, platform.frame)) {
// 發生碰撞
break;
}
}