2013-03-14 71 views
0

我正在研究iPhone應用程序,而在我的應用程序中有一個從屏幕頂部移動到底部的對象。爲此,我使用CAD顯示鏈接。一旦對象離開屏幕,它應該重新啓動它的路線。我遇到的問題是每次對象重新啓動它的路由時,它都會加速。這樣繼續下去,直到物體變得如此之快以至於幾乎看不到它。任何想法,爲什麼發生這種情況,以及如何阻止它?任何幫助表示感謝,提前致謝!每次調用方法時,CAD顯示鏈接似乎都在加快速度

-(void)spawnButton{ 

int x = (arc4random() % (240) + 40; 
int y = -100; 

button1.center = CGPointMake(x,y); 

displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveObject)]; 
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; 

} 



-(void) moveObject { 

int z = 1; 


button1.center = CGPointMake(button1.center.x , button1.center.y +z); 



if (button1.center.y >= 480) { 

    [self spawnButton]; 

} 
} 

回答

1

您將在每次致電spawnButton時創建新的顯示鏈接。如果您沒有做任何操作來從運行循環中刪除舊的顯示鏈接,則舊的顯示鏈接將繼續發送moveObject:消息。因此,在兩次撥打spawnButton後,您將在每個視頻幀中收到兩條moveObject:消息,並且在三次呼叫後,每個視頻幀將收到三條moveObject:消息,依此類推。

0

我似乎已經通過使顯示鏈接無效每次重新啓動對象路由來解決問題。

if (button1.center.y >= 480) { 

[self spawnButton]; 


[displaylink invalidate]; 

} 
} 
相關問題