I'm working on a little game that divides the ipad screen in a lower half and upper half. each player has his own section to play with (one finger play). I had to enable multitouch on the view because, when two players would hit the screen at the same time, only the methods for 1 player would be executed or none at all.
however this results in weird behavior of methods firing twice when 2 players do exactly the same action. this is how it works now: it checks if the players touched an object in the view. if they have, a method fires. if a player hasen't touched an object but the view itself, another method fires. it checks whether the view is touched in the upper or lower half to distinct the touches from player one or player two.
I've been thinking of a solution but I'm not sure what is a good way to solve this. maybe I should have to separate views (transparent) for each player so I can more easily distinct the touches? here's my touchesBegan method.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableSet *touchedCirclesPlayerOne = [NSMutableSet set];
NSMutableSet *touchedCirclesPlayerTwo = [NSMutableSet set];
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInView:self.view];
for (Circle *circle in playerOneCircles) {
if ([circle.layer.presentationLayer hitTest:touchLocation]) {
[touchedCirclesPlayerOne addObject:circle];
}
}
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInView:self.view];
for (Circle *circle in playerTwoCircles) {
if ([circle.layer.presentationLayer hitTest:touchLocation]) {
[touchedCirclesPlayerTwo addObject:circle];
}
}
}
if (touchedCirclesPlayerOne.count) {
NSLog(@"test");
for (Circle *circle in touchedCirclesPlayerOne) {
[circle playerTap:nil];
}
} else if (touchedCirclesPlayerTwo.count) {
NSLog(@"test2");
for (Circle *circle in touchedCirclesPlayerTwo) {
[circle SecondPlayerTap:nil];
}
} else {
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width, self.view.frame.size.height/2), touchLocation)) {
NSLog(@"wrong player 1");
[[NSNotificationCenter defaultCenter] postNotificationName:@"wrong player 1" object:self];
} else {
NSLog(@"wrong player 2");
[[NSNotificationCenter defaultCenter] postNotificationName:@"wrong player 2" object:self];
}
}
}
}
}
here's a little schematic. this all works except when two players do the same thing. it fires the same action twice for each player.