Zack Roderick
Zack Roderick

Reputation: 11

Enabling multi-touch in game?

I am creating a simple pong game in Xcode. I have a 2 player version and both paddles can be moved and function perfectly. The game is flawless except for the fact that you cannot move both paddles at the same time, rendering the 2 player mode useless.

How can I enable both paddles to be moved at the same time? I've already tried selecting the "Multiple Touch" button in Interface Builder but it does nothing and im not quite sure it is even the correct route into enabling multi touch as I want it.

Also, my game is a View-Based Application if that matters.

Thanks!

Upvotes: 1

Views: 3720

Answers (3)

Pheu Verg
Pheu Verg

Reputation: 230

self.view.multipleTouchEnabled = YES;

by default it is No

Upvotes: 0

Lucas Derraugh
Lucas Derraugh

Reputation: 7049

From my experience (which isn't that much). You can create 2 UIViews for the 2 paddles, touching in one view will move one paddle, while touching in the other will move the other paddle. I hope this helps.

If you don't know how to split the views, you can simply make it identify 2 touches

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [[event touchesForView:zone1] anyObject];
UITouch *touch2 = [[event touchesForView:zone2] anyObject];
if (touch1)
    touchOffset1 = paddle1.center.x - [touch1 locationInView:touch1.view].x;
if (touch2)
    touchOffset2 = paddle2.center.x - [touch2 locationInView:touch2.view].x;
}

This you can use, it probably isn't the most productive, but it does work if you can't figure out how to split the touches.

Upvotes: 1

Dave
Dave

Reputation: 3448

EDIT: I mis-read the question. Added code snippet.

Here's the code I use to extract all touches when I get a touchesBegan event:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSArray *touchesArray = [touches allObjects];
    NSUInteger nNumTouches = [touchesArray count];
    UITouch *touch;
    CGPoint ptTouch;
    for (int nTouch = 0;  nTouch < nNumTouches;  nTouch++)
    {
        touch = [touchesArray objectAtIndex:nTouch];
        ptTouch = [touch locationInView:self.view];

        // Do stuff with the touch
    }
}

and similarly for touchesEnded and touchesMoved

Upvotes: 1

Related Questions