未检测到SkSpriteNode冲突

时间:2014-11-15 11:09:02

标签: ios sprite-kit

我使用Sprite Kit检测两个对象之间的碰撞。这是我如何定义他们的位掩码。

static const uint32_t puffinCategory = 0x1 << 0;
static const uint32_t planeCategory = 0x1 << 1;

这是关于如何设置海鹦和飞机物理体的代码。 对于海雀

SKSpriteNode *PuffinNode = [[SKSpriteNode alloc]initWithImageNamed:@"puffin"];
    PuffinNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:PuffinNode.size];
    PuffinNode.physicsBody.usesPreciseCollisionDetection = YES;
    PuffinNode.physicsBody.categoryBitMask = puffinCategory;
    PuffinNode.physicsBody.dynamic = NO;
    PuffinNode.physicsBody.collisionBitMask = puffinCategory;
    PuffinNode.physicsBody.contactTestBitMask = planeCategory;


    [PuffinNode setZPosition:1.5];

飞机

SKSpriteNode *planeSpriteNode = [[SKSpriteNode alloc]initWithImageNamed:planeStringFileName];
planeSpriteNode.position = CGPointMake(0, self.view.frame.size.height*-1);
planeSpriteNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:planeSpriteNode.size];
planeSpriteNode.physicsBody.usesPreciseCollisionDetection = YES;
planeSpriteNode.physicsBody.categoryBitMask = planeCategory;
planeSpriteNode.physicsBody.dynamic = NO;
planeSpriteNode.physicsBody.collisionBitMask = puffinCategory;
planeSpriteNode.physicsBody.contactTestBitMask = puffinCategory;

这是我的委托方法didBeginContact:

的实现
-(void)didBeginContact:(SKPhysicsContact *)contact{
    NSLog(@"collission method run");
    SKPhysicsBody *firstBody, *secondBody;
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask){
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }else{
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }

    if ((firstBody.categoryBitMask & puffinCategory) != 0 && (secondBody.categoryBitMask & planeCategory) != 0 ){
        NSLog(@"collission occured");
    }   
}

我没有看到方法记录,如果它被调用,当两个精灵碰撞时我也没有看到日志。

2 个答案:

答案 0 :(得分:2)

你设置

PuffinNode.physicsBody.dynamic = NO;

planeSpriteNode.physicsBody.dynamic = NO;

两个静态物体不能碰撞,至少一个应该是动态的

答案 1 :(得分:0)

一些问题:

1)您是否设置了SKScene的physicsWorld.contactDelegate? 2)您的两个节点都没有动态。如果你想让它们在物理世界中互动,你应该根据物理定律制作它们。其中一个至少必须是动态的。 3)您的collisionBitMask设置不正确。