2013-03-14 72 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]; 

} 
} 
相关问题