我正在为一部 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;
除非我在视图部分遗漏了一些简单的逻辑,否则我迷路了。有什么建议么?