在應用程序中,每次運行時我都會將隨機幀放到正方形中。我用這個邏輯,以確保CGRectIntersectsRect邏輯錯誤
一)每平方米是不是太靠近玩家
和
B)每平方包含在屏幕 C的視圖中)沒有方接觸的任何其他方
for(UIButton* button in squareArray) {
BOOL shouldContinue = YES;
do {
int randX = arc4random() % 321;
int randY = arc4random() % 481;
button.frame = CGRectMake(randX, randY, button.frame.size.width, button.frame.size.height);
CGRect playerRect = CGRectMake(100, 180, 120, 120);
for(UIButton* b in squareArray)
if(!CGRectIntersectsRect(b.frame, button.frame) &&
!CGRectIntersectsRect(button.frame, playerRect) &&
CGRectContainsRect(self.view.frame, button.frame)) {
shouldContinue = NO;
}
} while (shouldContinue);
}
使用此代碼,我期望squareArray中的每個方塊都將(完成循環後)完全位於視圖的邊界內,不與數組中的任何其他按鈕相交,並且完全位於playerRect矩形的邊界,它是屏幕中心的120 x 120方形。我的代碼錯了嗎?因爲我沒有得到這些功能。
編輯:我確實得到了這個方法的一個理想的特徵:沒有正方形與playerRect相交。但我仍然看到方塊互相重疊,並且正方形部分偏離視野。
編輯2:
我做這些更改,以嵌套的循環:
for(UIButton* b in squareArray)
if(![b isEqual:button]) {
if(CGRectIntersectsRect(b.frame, button.frame) ||
CGRectIntersectsRect(button.frame, playerRect) ||
!CGRectContainsRect(CGRectMake(10, 10, 300, 460), button.frame))
shouldContinue = YES;
else
shouldContinue = NO;
}
而且現在的廣場一直是稍微修改(小)矩形的視圖中,他們從來沒有相交玩家廣場。好極了。但它們仍然相互重疊。爲什麼?
您沒有提出問題或解釋您認爲您的代碼有問題。 –
Bahahaha對不起,我實際上在問一個問題之前意外地發佈了它,因爲我很匆忙。正在更新... –