不相互影响的不同目标c

时间:2016-03-08 08:08:42

标签: ios objective-c sprite-kit multi-touch

我正在为Ipad上的两个玩家制作简单的游戏,就像乒乓球一样,当一个手指在屏幕上时,玩家可以用他的球拍做出反应,但当第二根手指在他的球拍上时,他无法移动他的球拍,但他相反,移动第一个桨,我设置了一些NSLog,它说两个都移动了,但这不对,这是我的代码示例:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
/* Called when a touch begins */
    for (UITouch *touch in touches) {
//First player
CGPoint touchLocation = [touch locationInNode:self];
SKPhysicsBody* body = [self.physicsWorld bodyAtPoint:touchLocation];

if (body && [body.node.name isEqualToString: paddleCategoryName]) {
    NSLog(@"Began touch on first paddle");
    self.isFingerOnPaddle = YES;
}
//Second player
CGPoint secondTouchLocation = [touch locationInNode:self];
SKPhysicsBody* secondbody = [self.physicsWorld bodyAtPoint:secondTouchLocation];
if (secondbody && [secondbody.node.name isEqualToString: secondPaddleCategoryName]) {
    NSLog(@"Began touch on second paddle");
    self.isSecondFingerOnPaddle = YES;
}
    }
 }

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
for (UITouch *touch in touches) {
    //for first player
    if (self.isFingerOnPaddle) {
        CGPoint touchLocation = [touch locationInNode:self];
        CGPoint previousLocation = [touch previousLocationInNode:self];
        SKSpriteNode* paddle = (SKSpriteNode*)[self childNodeWithName: paddleCategoryName];
        int paddleY = paddle.position.y + (touchLocation.y - previousLocation.y);
        paddleY = MAX(paddleY, paddle.size.height/2);
        paddleY = MIN(paddleY, self.size.height - paddle.size.height/2);
        paddle.position = CGPointMake(paddle.position.x, paddleY);
        NSLog(@"First paddle moving");

    }
    //for second player
    if (self.isSecondFingerOnPaddle) {
        CGPoint touchLocation = [touch locationInNode:self];
        CGPoint previousLocation = [touch previousLocationInNode:self];
        SKSpriteNode* secondPaddle = (SKSpriteNode*)[self childNodeWithName: secondPaddleCategoryName];
        int secondPaddleY = secondPaddle.position.y + (touchLocation.y - previousLocation.y);
        secondPaddleY = MAX(secondPaddleY, secondPaddle.size.height/2);
        secondPaddleY = MIN(secondPaddleY, self.size.height - secondPaddle.size.height/2);
        secondPaddle.position = CGPointMake(secondPaddle.position.x, secondPaddleY);
        NSLog(@"Second paddle moving");
    }
}
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
self.isFingerOnPaddle = NO;
self.isSecondFingerOnPaddle = NO;
 }

我做错了什么,我需要更改为代码工作,就像它应该

2 个答案:

答案 0 :(得分:1)

你的逻辑是错误的,只需添加一个touchArray属性来存储第一次触摸和第二次触摸,在触摸开始时设置它然后确定何时移动触摸。

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    /* Called when a touch begins */
    for (UITouch *touch in touches) {

        CGPoint touchLocation = [touch locationInNode:self];
        SKSpriteNode *node = (SKSpriteNode *)[self nodeAtPoint:touchLocation];

        if (node && [node.name isEqualToString: paddleCategoryName]) {
            NSLog(@"Began touch on first paddle");

            [self.touchArray replaceObjectAtIndex:0 withObject:touch];

        }else if (node && [node.name isEqualToString: secondPaddleCategoryName]) {
            NSLog(@"Began touch on second paddle");

            [self.touchArray replaceObjectAtIndex:1 withObject:touch];

        }

    }
}

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInNode:self];

        UITouch *firstTouch = self.touchArray[0];
        UITouch *secondTouch = self.touchArray[1];

        SKSpriteNode *paddle = [[SKSpriteNode alloc] init];

        if (touch == firstTouch) {
            CGPoint previousLocation = [touch previousLocationInNode:self];
            SKSpriteNode* paddle = (SKSpriteNode*)[self childNodeWithName: paddleCategoryName];
            int paddleY = paddle.position.y + (touchLocation.y - previousLocation.y);
            paddleY = MAX(paddleY, paddle.size.height/2);
            paddleY = MIN(paddleY, self.size.height - paddle.size.height/2);
            paddle.position = CGPointMake(paddle.position.x, paddleY);
            NSLog(@"First paddle moving");

        }else if (touch == secondTouch) {
            CGPoint touchLocation = [touch locationInNode:self];
            CGPoint previousLocation = [touch previousLocationInNode:self];
            SKSpriteNode* secondPaddle = (SKSpriteNode*)[self childNodeWithName: secondPaddleCategoryName];
            int secondPaddleY = secondPaddle.position.y + (touchLocation.y - previousLocation.y);
            secondPaddleY = MAX(secondPaddleY, secondPaddle.size.height/2);
            secondPaddleY = MIN(secondPaddleY, self.size.height - secondPaddle.size.height/2);
            secondPaddle.position = CGPointMake(secondPaddle.position.x, secondPaddleY);
            NSLog(@"Second paddle moving");
        }


    }
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
    for (UITouch *touch in touches) {
        if (touch == self.touchArray[0]) {
            [self.touchArray replaceObjectAtIndex:0 withObject:@""];
        }else if (touch == self.touchArray[1]) {
            [self.touchArray replaceObjectAtIndex:1 withObject:@""];

        }
    }

    self.isFingerOnPaddle = NO;
    self.isSecondFingerOnPaddle = NO;
}

答案 1 :(得分:1)

更好的方法是使用子类paddle并在paddle子类中执行触摸代码。这消除了必须循环通过触摸阵列以查看您的拨片是否被触摸,而且它使代码更整洁,更容易阅读。唯一的问题是你必须以不同的方式设置节点设置的样式,因为移动的触摸不会在桨的外部工作。

这是你如何设计风格

  

1)Subclass Paddle to SKNode
  2)为您的实际paddle gfx创建一个SKSpriteNode子项   3)桨架应设置在场景的宽度上,并允许玩家触摸的高度(可能是球拍gfx的高度)
  4)覆盖Paddle内的触摸代码,该组中的所有触摸代码仅应与桨叶触摸的内容相关   5)在这些覆盖中执行所有滑动逻辑

现在您已针对所寻找的行为定义了Paddle,您可以将其添加到您喜欢的场景中。

关于这种方法的好处是你消除了很多重复的代码,(因为两边的桨叶表现不同)如果你想增加一些乐趣,你可以在两侧添加更多的桨以提供更多的变化。 (每个玩家在比赛场地中间有一个球拍,底部可以独立移动)