0

我正在为一部 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;

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

4

1 回答 1

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;
于 2013-08-14T23:58:23.833 回答