开发者

Enabling multi-touch in game?

开发者 https://www.devze.com 2023-02-07 21:31 出处:网络
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

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!


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


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.


self.view.multipleTouchEnabled = YES;

by default it is No

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号