Can't drag 2 imageViews at the same time using touchesMoved
开发者_如何学PythonI'm making a pong game and when I play as 2 players the images wont move at the same time: I have to release the one to move another.
Here is the code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesMoved:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x > 240) {
CGPoint ylocation = CGPointMake(player.center.x, location.y);
player.center = ylocation;
}
if (mode == kdual) {
if (location.x < 240) {
CGPoint ylocation = CGPointMake(cpu.center.x, location.y);
cpu.center = ylocation;
}
}
}
You only check the position of one touch object. You should check all touch objects, and move the images accordingly. Something like this:
for (UITouch * touch in [touches allObjects])
{
// Check position of touch and move the images
CGPoint location = [touch locationInView:touch.view];
if (location.x > 240) {
CGPoint ylocation = CGPointMake(player.center.x, location.y);
player.center = ylocation;
}
if (mode == kdual) {
if (location.x < 240) {
CGPoint ylocation = CGPointMake(cpu.center.x, location.y);
cpu.center = ylocation;
}
}
}
精彩评论