多个冲突spritekit

时间:2014-08-11 14:29:39

标签: ios sprite-kit

我正在尝试制作一个SpriteKit游戏,但我无法弄清楚或找到多个碰撞检测的好例子。
我的代码:     

-(void)didBeginContact:(SKPhysicsContact *)contact{

SKPhysicsBody *firstBody;
SKPhysicsBody *secondBody;

if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
    firstBody = contact.bodyA;
    secondBody = contact.bodyB;
}
else
{
    firstBody = contact.bodyB;
    secondBody = contact.bodyA;
}

if ((firstBody.categoryBitMask & bulletCategory) != 0) {
    [self updateScore];
    SKNode *applenode = [self childNodeWithName:(@"Apple")];
    applenode.removeFromParent;
    [self updateScore];
    SKScene *sampleScene = [[Level2 alloc] initWithSize:self.size];
    SKTransition *transition = [SKTransition flipVerticalWithDuration:0.5];
    [self.view presentScene:sampleScene transition:transition];

}

很好地应对了一次碰撞,但我似乎无法将其用于多次碰撞。

1 个答案:

答案 0 :(得分:0)

//对于多次碰撞,您可以添加类似

的代码
  • (void)didBegin联系人:(SKPhysicsContact *)联系 {

    CGPoint contactPoint=CGPointMake(0, 0);
    //multiple collision
    if (contact.bodyA.categoryBitMask || contact.bodyB.categoryBitMask)
    {
        //first collision
        if(([contact.bodyA.node.name isEqualToString:@"hero"] && [contact.bodyB.node.name isEqualToString:@"coin"] ) || ([contact.bodyA.node.name isEqualToString:@"coin"] && [contact.bodyB.node.name isEqualToString:@"hero"]) )
        {
            if([contact.bodyA.node.name isEqualToString:@"coin"] )
            {
    
                [contact.bodyA.node removeFromParent];
            }
            else
            {
                [contact.bodyB.node removeFromParent];
            }
    
        }
        //second collision
        else if(([contact.bodyA.node.name isEqualToString:@"hero"] && [contact.bodyB.node.name isEqualToString:@"enemy"] ) || ([contact.bodyA.node.name isEqualToString:@"enemy"] && [contact.bodyB.node.name isEqualToString:@"hero"]) )
        {
            if([contact.bodyA.node.name isEqualToString:@"hero"] )
            {
    
                [contact.bodyA.node removeFromParent];
            }
            else
            {
                [contact.bodyB.node removeFromParent];
            }
    
        }
    }
    

    }