两个可拖动的对象无法正常工作

时间:2014-04-29 20:46:07

标签: uiimageview touch draggable pong

我根据教程制作Pong游戏,但教程只向您展示如何对抗计算机。现在我试图让两个酒吧可以为玩家VS玩家游戏拖延。

我尝试了这个,但这只会使两个棒同时移动。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *Drag = [[event allTouches] anyObject];
Player1.center = [Drag locationInView:self.view];

if (Player1.center.y > 533) {
    Player1.center = CGPointMake(Player1.center.x, 533);
}

if (Player1.center.y < 533) {
    Player1.center = CGPointMake(Player1.center.x, 533);
}

if (Player1.center.x < 50) {
    Player1.center = CGPointMake(50, Player1.center.y);
}

if (Player1.center.x > 270) {
    Player1.center = CGPointMake(270, Player1.center.y);
}

UITouch *Drag2 = [[event allTouches] anyObject];
Player2.center = [Drag2 locationInView:self.view];

if (Player2.center.y > 20) {
    Player2.center = CGPointMake(Player2.center.x, 20);
}

if (Player2.center.y < 20) {
    Player2.center = CGPointMake(Player2.center.x, 20);
}

if (Player2.center.x < 50) {
    Player2.center = CGPointMake(50, Player2.center.y);
}

if (Player2.center.x > 270) {
    Player2.center = CGPointMake(270, Player2.center.y);
}

}

1 个答案:

答案 0 :(得分:0)

嗯,任何人都可以帮助我,但我做到了。我会把代码放在有人遇到同样问题的情况下。首先,我需要解释你需要在游戏的View Controller中添加两个视图,并在.h文件中声明它们:

IBOutlet UIView *zone1;
IBOutlet UIView *zone2;

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{




    UITouch *Drag = [[event touchesForView:zone1] anyObject];
    UITouch *Drag2 = [[event touchesForView:zone2] anyObject];

    if (Drag) {


    Player1.center = [Drag locationInView:self.view];

    if (Player1.center.y > 533) {
        Player1.center = CGPointMake(Player1.center.x, 533);
    }

    if (Player1.center.y < 533) {
        Player1.center = CGPointMake(Player1.center.x, 533);
    }

    if (Player1.center.x < 50) {
        Player1.center = CGPointMake(50, Player1.center.y);
    }

    if (Player1.center.x > 270) {
        Player1.center = CGPointMake(270, Player1.center.y);
    }
}

    if (Drag2) {

    Player2.center = [Drag2 locationInView:Drag2.view];

    if (Player2.center.y > 30) {
        Player2.center = CGPointMake(Player2.center.x, 30);
    }

    if (Player2.center.y < 30) {
        Player2.center = CGPointMake(Player2.center.x, 30);
    }

    if (Player2.center.x < 50) {
        Player2.center = CGPointMake(50, Player2.center.y);
    }

    if (Player2.center.x > 270) {
        Player2.center = CGPointMake(270, Player2.center.y);
    }
    }
}

记住,UIViews必须是View Controller的一半,一半是上半部分,另一半是下半部分。