在一部手机上的两个玩家乒乓球游戏

时间:2013-08-14 04:11:51

标签: ios multi-touch uitouch

我正在为一部iPhone制作类似乒乓球的游戏,其中两名玩家从手机的每一端控制一个球拍。我无法独立移动桨。我让他们两个都用以下代码完全相同:

UITouch *touch1 = [[event allTouches] anyObject];
CGPoint location = [touch1 locationInView:touch1.view];
CGPoint yLocation = CGPointMake(p1_paddle.center.x,location.y);
p1_paddle.center = yLocation;

UITouch *touch2 = [[event allTouches] anyObject];
CGPoint location2 = [touch2 locationInView:touch2.view];
CGPoint yLocation2 = CGPointMake(p2_paddle.center.x,location2.y);
p2_paddle.center = yLocation2;

我做了一些研究并了解到,通过将视图分成两个不同的片段,每个播放器一个,可以实现这一点。这是我使用的代码,但它不起作用,它将两个拨片移动到屏幕的一侧,甚至没有移动:

UITouch *touch1 = [[event touchesForView: p1_field] anyObject];
CGPoint location = [touch1 locationInView:touch1.view];
CGPoint yLocation = CGPointMake(p1_paddle.center.x,location.y);
p1_paddle.center = yLocation;

UITouch *touch2 = [[event touchesForView: p2_field] anyObject];
CGPoint location2 = [touch2 locationInView:touch2.view];
CGPoint yLocation2 = CGPointMake(p2_paddle.center.x,location2.y);
p2_paddle.center = yLocation2;

除非我在视图部分缺少一些简单的逻辑,否则我迷路了。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

好的,经过一些研究和玩弄后,我终于开始工作了。下面是代码。这是手机在横向方向。

for (UITouch *touch in [event allTouches]) {
    CGPoint location = [touch locationInView:touch.view];
    if (location.x < 240) {
        CGPoint yLocation = CGPointMake(p1_paddle.center.x,location.y);
        p1_paddle.center = yLocation;
    }

    if (location.x > 240) {
        CGPoint yLocation2 = CGPointMake(p2_paddle.center.x,location.y);
        p2_paddle.center = yLocation2;
    }
}

还要记住viewDidLoad方法,

self.view.multipleTouchEnabled = YES;