开发者

Can't drag 2 imageViews at the same time using touchesMoved

开发者 https://www.devze.com 2023-04-12 20:29 出处:网络
开发者_如何学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.

开发者_如何学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;
           }
     } 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号