在游戏中启用多点触控?

时间:2011-01-28 22:25:46

标签: xcode multi-touch

我正在Xcode中创建一个简单的乒乓球游戏。我有一个2人版本,两个拨片都可以移动,功能完美。游戏是完美的,除了你不能同时移动两个桨,使2玩家模式无用。

如何同时启用两个拨片? 我已经尝试在Interface Builder中选择“多次触摸”按钮,但它什么也没做,我不太确定它是否是我想要的启用多点触控的正确途径。

此外,如果重要的话,我的游戏是基于视图的应用程序。

谢谢!

3 个答案:

答案 0 :(得分:1)

根据我的经验(不是那么多)。您可以为2个拨片创建2个UIViews,在一个视图中触摸将移动一个拨片,而在另一个视图中触摸将移动另一个拨片。我希望这会有所帮助。

如果你不知道如何拆分视图,你可以简单地识别2个触摸

- (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;
}

你可以使用它,它可能不是最有效率的,但如果你无法弄清楚如何分割触摸它确实有效。

答案 1 :(得分:1)

编辑:我误读了这个问题。添加了代码段。

以下是我在获取touchesBegan事件时用于提取所有触摸的代码:

- (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
    }
}

并且类似于touchesEnded和touchesMoved

答案 2 :(得分:0)

self.view.multipleTouchEnabled = YES;

默认情况下为否

相关问题