Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 不相互反应的不同接触目标c_Ios_Objective C_Sprite Kit_Multi Touch - Fatal编程技术网

Ios 不相互反应的不同接触目标c

Ios 不相互反应的不同接触目标c,ios,objective-c,sprite-kit,multi-touch,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

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

我做错了什么,我需要对我的代码工作做什么更改,比如它应该

您的逻辑是错误的,只需添加一个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;
}

一个更好的方法是将Padle子类化,并在Padle子类中编写触摸代码。这样就不必在触摸阵列中循环,以查看您的划桨是否被触摸,而且它使代码更加整洁,易于阅读。唯一的问题是,您必须对节点设置进行稍微不同的样式设置,因为“触动移动”将无法在挡板之外工作

这是你的风格

1) 子类桨到SKNode
2) 为您的实际桨gfx创建SKSpriteNode子级
3) 拨片框应设置为场景的宽度,并具有允许玩家触摸的高度(可能是拨片gfx的高度)
4) 覆盖拨杆内的触摸代码,设置中的所有触摸代码应仅与拨杆所触摸的内容相关
5) 在这些覆盖中执行所有滑动逻辑

现在,您已经为要查找的行为定义了拨片,您可以随时将其添加到场景中


这种方法的优点在于消除了大量重复代码(因为两侧的划片行为不同),如果您想增加一些乐趣,可以在两侧添加更多划片以提供更多的变化。(每个玩家在游戏场的中间有一个桨,而底部可以独立移动)

对不起,我不知道如何设置数组,你能告诉我吗?将一个字符串放入一系列触控中(并在触控中施放,即说谎)是一个很好的主意。@Eiko您有更好的建议吗?一个非常简单的方法是将这两个触控存储在单独的属性中,然后将其置零。但将NSString视为UITouch几乎肯定会在以后不考虑它时崩溃。谢谢你的建议,我肯定会使用你关于添加更多挡板的建议,这听起来很棒。重点是编写可重用的代码,避免重复