iPadの画面を下半分と上半分に分割するちょっとしたゲームに取り組んでいます。各プレーヤーには、自分のセクションがあります (ワンフィンガープレイ)。ビューでマルチタッチを有効にする必要がありました。これは、2 人のプレーヤーが同時に画面にヒットすると、1 人のプレーヤーのメソッドのみが実行されるか、まったく実行されないためです。
ただし、これにより、2 人のプレイヤーがまったく同じアクションを実行すると、メソッドが 2 回起動するという奇妙な動作が発生します。これが現在の動作方法です。プレイヤーがビュー内のオブジェクトに触れたかどうかを確認します。存在する場合、メソッドが起動します。プレーヤーがオブジェクトではなくビュー自体に触れた場合、別のメソッドが起動します。プレーヤー 1 またはプレーヤー 2 からタッチを区別するために、ビューが上半分または下半分でタッチされているかどうかを確認します。
私は解決策を考えてきましたが、これを解決するための良い方法が何であるかわかりません。タッチをより簡単に区別できるように、各プレーヤーのビュー (透明) を分離する必要があるのではないでしょうか? これが私の touchesBegan メソッドです。
- (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];
}
}
}
}
}
ここに少し概略図があります。2 人のプレイヤーが同じことを行う場合を除いて、これはすべて機能します。各プレイヤーに対して同じアクションを 2 回実行します。