我目前使用两个类:Player 和 Ball(Monster 类的儿子)。使用 d-pad,我可以在屏幕上移动球员,并且球在屏幕上不断弹跳。在 ViewController 中,我有一个 NSMutableArray 的球员和一个 NSMutableArray 的球。
Player *m = [[Car alloc]init];
[players addObject:m];
[self.view addSubview:m.image];
joypad = [[Joypad alloc] initWithPlayer:players[0]];
[self.view addSubview: joypad.pad];
[self.view addSubview:joypad.movingPad];
monsters = [NSMutableArray arrayWithCapacity:MAX];
for(int i = 0; i < MAX; ++i)
{
int radius = rand()%100;
int deltax = rand()%5 + 1;
int deltay = rand()%5 +1;
Monster *ball = [[Ball alloc] initWithRadius:radius andDX:deltax DY:deltay];
[self.view addSubview:ball.image];
[ball startMoving];
}
我如何检测球与球和球与球员之间的碰撞? 在此处输入链接描述
我尝试在 VievController 的 viewDidLoad 中调用此方法(checkCllisions),但它没有显示任何消息:
-(void) checkCollisions{
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(collisions) userInfo:nil repeats:YES];
}
-(void) collisions{
for(int i = 0; i < [players count]; i++){
Player *p = players[i];
CGRect f = CGRectMake(p.image.frame.origin.x, p.image.frame.origin.y, p.image.frame.size.width, p.image.frame.size.height);
for (int k = 0; k < [monsters count]; i++){
Monster *m = monsters[i];
CGRect fm = CGRectMake(m.image.frame.origin.x, m.image.frame.origin.y, m.image.frame.size.width, m.image.frame.size.height);
if(CGRectIntersectsRect(f, fm))
NSLog(@"collision");
}
}
}