我有一個「雨」像動畫從屏幕的頂部移動到底部(幾個圖像不斷從頂部下降)。我也有一個籃子圖像,由手指在底部控制。我希望應用程序計算任何放置圖像和籃子之間的「碰撞」數量,如果它們碰撞,請移除圖像。太多CGRectIntersectsRect碰撞計數
我使用兩個計時器:一個不斷從頂部放下圖像。我把這些圖像放在一個數組中。然後我有另一個計時器,每0.001,檢查碰撞。
我每次碰到兩個物體都能夠計數,但問題是,它的計數方式太多了!例如,每次視覺碰撞都會計算出985次碰撞。那我在我的代碼做錯了?: 定時器:
[NSTimer scheduledTimerWithTimeInterval:(0.001) target:self selector:@selector(onCollisionCheckTimer) userInfo:nil repeats:YES];
碰撞檢查:
- (void)onCollisionCheckTimer{
for (x = 0; x<100; x++){
CALayer *layer = appleView[x].layer.presentationLayer;
if(CGRectIntersectsRect(basketView.frame, layer.frame)) {
collision++;
printf("%i\n", collision);
[appleView[x] removeFromSuperview];
}
}
}
的printf("%i", collision);
在數百輸出號碼每一次碰撞
或者在AppleView陣列中的每個蘋果視圖登陸籃筐時刪除它們。此外,使用'CADisplayLink'而不是'NSTimer'。 'CADisplayLink'與顯示刷新定時器同步,這也是驅動Core Animation更新的原因。 –
謝謝你們的幫助和信息! – user1861051
我將如何做superview屬性檢查? – user1861051