Pergunta

I am making a pong-like game for one iPhone where two players control a paddle from each end of the phone. I'm having trouble moving paddles independently. I am getting them both to move exactly the same with the following code:

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;

I have done some researched and learned that this may be possible by splitting the view into two different segments, one for each player. This is the code I used, but it isn't working, it moves both paddles to one side of the screen up in a corner, not even moving:

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;

Unless I'm missing some simple logic in the view part, I'm lost. Any suggestions?

Foi útil?

Solução

OK, after doing some more research and toying around, I finally got it working. Below is the code. This is with the phone in landscape orientation.

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

ALSO REMEMBER in your viewDidLoad method,

self.view.multipleTouchEnabled = YES;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top