Reputation: 57
I have made an app and i want to make it multi touch compatible. i have tried looking around but the answers aren't specific to mine. Here is what i do:
1) My coding:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesMoved:touches withEvent:event];
if (gameState == kGameStatePaused) {(gameState = kGameStateRunning);}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStateRunning) {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if(location.x > 400) {
CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y);
playerPaddle.center = yLocation;
}
if (gameStyle == kGameStyleTwoP) {
if(location.x < 100) {
CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y);
computerPaddle.center = yLocation2;
}
}
}
2) I have gone onto Interface Builder and checked the box for multitouch enabled
3) I build and run my app and it opens properly and when i go to test the multitouch i hold "option key" and click and move the mouse
4) (i am trying to make computerPaddle and playerPaddle both move) but only one of the works at a time
I have to tried to fix it but i can't understand where i am going wrong.
Any help is useful. THX.
Upvotes: 1
Views: 5918
Reputation: 46608
look this line
UITouch *touch = [[event allTouches] anyObject];
you only take one touch and ignore rest and thats why only one thing can move
so replace with a for loop should fix the problem
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameState == kGameStateRunning) {
for (UITouch *touch in [event allTouches]) {
CGPoint location = [touch locationInView:touch.view];
if(location.x > 400) {
CGPoint yLocation = CGPointMake(playerPaddle.center.x, location.y);
playerPaddle.center = yLocation;
}
if (gameStyle == kGameStyleTwoP) {
if(location.x < 100) {
CGPoint yLocation2 = CGPointMake(computerPaddle.center.x, location.y);
computerPaddle.center = yLocation2;
}
}
}
}
Upvotes: 2
Reputation: 3596
There is a property named multipleTouchEnabled
on the UIView that you can set to YES to enable it (defaults NO).
Also, you should loop to treat all touches in the touches
set that you receive in touchesMoved
.
Upvotes: 10