物理机构没有响应位掩码设置

时间:2014-02-02 00:46:42

标签: ios sprite-kit skphysicsbody

我在物理主体上设置碰撞位掩码,这样它就不会发生冲突,但仍然会发生冲突。

-(void) createPlayer{
    player = [SKSpriteNode spriteNodeWithImageNamed:@"GonGonRed"];
    player.position = CGPointMake(40, 100);
    player.size = CGSizeMake(35*self.frame.size.height/320, 35*self.frame.size.height/320);
    player.zPosition = 7;    
    player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:player.size.width/2-1];
    player.physicsBody.dynamic = NO;
    player.physicsBody.allowsRotation = NO;
    player.physicsBody.usesPreciseCollisionDetection = YES;
    player.physicsBody.categoryBitMask = 1;
    player.physicsBody.contactTestBitMask = 4;
    player.physicsBody.collisionBitMask = 2;
    player.physicsBody.mass = 0.013963;
    [self addChild:player];
}


SKSpriteNode *wheel = [SKSpriteNode spriteNodeWithImageNamed:@"wheel"];
wheel.size = CGSizeMake(newwidth, newheight);
wheel.position = CGPointMake(px, py);
wheel.name = @"wheel";    
wheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:newwidth/2];
wheel.physicsBody.dynamic = NO;
wheel.physicsBody.restitution = obstacleRestitution;
wheel.physicsBody.usesPreciseCollisionDetection = YES;
wheel.physicsBody.categoryBitMask = 3;
wheel.physicsBody.collisionBitMask = 0;
wheel.physicsBody.contactTestBitMask = 0;
wheel.zPosition = 5;

轮子和球员不应该碰撞,但他们是。我

1 个答案:

答案 0 :(得分:3)

这是由以下行引起的。

wheel.physicsBody.categoryBitMask = 3;

SpriteKit通过执行逻辑AND操作将正文的categoryBitMask与其他正文的collisionBitMask进行比较。 当结果为非零值时,将触发碰撞。

在这种情况下,SpriteKit会将wheel的categoryBitMask与玩家的collisionBitMask进行比较,如下所示。

轮子的categoryBitMask是0x011(= 3)
玩家的collisionBitMask是0x010(= 2)
AND运算结果为0x010。这是非零,因此发生了碰撞。

所以你应该改变wheel的categoryBitMask值,以避免逻辑AND操作的结果变为Non-Zero,如下所示。

wheel.physicsBody.categoryBitMask = 8;

此外,必须将动态属性设置为YES。