我正在开发一个简单的游戏,我需要在屏幕外移动气泡,如下图所示。
在上面显示的图像中气泡7和10,4和9相互撞击。我需要泡泡在屏幕上移动而不会相互撞击。
我在循环中添加气泡并随机改变位置,如下所示。
for (int i = 0; i < 10; i ++) {
self.bubbleButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.bubbleButton.frame = CGRectMake(20.0, 50.0, 60.0, 60.0);
self.bubbleButton.tag = i+1;
[self.bubbleButton setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
[self.bubbleButton addTarget:self action:@selector(onTapOfBubble:) forControlEvents:UIControlEventTouchUpInside];
[self.bubbleButton setBackgroundImage:[UIImage imageNamed:@"circle"] forState:UIControlStateNormal];
[self.view addSubview:self.bubbleButton];
NSNumber *number = [NSNumber numberWithInt:i+1];
[UIView beginAnimations:nil context:(__bridge void * _Nullable)(number)];
[UIView setAnimationDuration:3.0];
CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);
CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height);
CGPoint squarePostion = CGPointMake(x, y);
self.bubbleButton.center = squarePostion;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)];
[UIView commitAnimations];
}
我无法弄清楚如何避免气泡相互撞击。有人可以建议我如何完成这项工作吗?