iOS SpriteKit Physic主体不会发生冲突

时间:2014-04-02 19:33:50

标签: ios sprite-kit physics

您好我有3个症状:

其中2个按预期完美互动,但当我尝试添加一些边界时,我的其他2个对象不会与边界发生碰撞:

SKShapeNode* node = [[SKShapeNode alloc]init];
CGRect rect = CGRectMake(0, 0, 270, 1);
node.path = [UIBezierPath bezierPathWithRect:rect].CGPath;
node.fillColor = SKColor.whiteColor;
node.strokeColor = nil;
node.position = CGPointMake(20, 50);

node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rect.size];
node.physicsBody.affectedByGravity = NO;
node.physicsBody.categoryBitMask = boundriesCat;
node.physicsBody.contactTestBitMask = blockCat | breakerCat;
node.physicsBody.collisionBitMask = blockCat | breakerCat;

[self addChild:node];

这里是另一个对象的init:

CGRect circle = CGRectMake(0, 0, 20, 20);
    self.path = [UIBezierPath bezierPathWithOvalInRect:circle].CGPath;
    if (code == 4)
        self.fillColor = SKColor.redColor;
    if (code == 5)
        self.fillColor = SKColor.greenColor;
    if (code == 6)
        self.fillColor = SKColor.blueColor;
    if (code == 7)
        self.fillColor = SKColor.yellowColor;
    self.strokeColor = nil;

    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:circle.size];
    self.physicsBody.dynamic = YES;
    self.physicsBody.categoryBitMask = breakerCat;
    self.physicsBody.contactTestBitMask = blockCat | breakerCat;
    self.physicsBody.collisionBitMask = boundriesCat;

我希望你可以帮助,第一天用精灵套装;(

1 个答案:

答案 0 :(得分:0)

检查您的collisionBitMasks。默认情况下,它们设置为0xFFFFFFFF(节点将与所有其他节点冲突)。但是,如果要显式设置冲突,则应将掩码设置为:

static const uint32_t kBoundariesCat = (0x01 << 0); // 0001
static const uint32_t kBreakerCat    = (0x01 << 1); // 0010
static const uint32_t kBlockCat      = (0x01 << 2); // 0100

static const uint32_t kBoundariesCatCollisionMask = kBreakerCat | kBlockCat;      // 0110
static const uint32_t kBreakerCatCollisionMask    = kBoundariesCat | kBlockCat;   // 0101
static const uint32_t kBlockCatCollisionMask      = kBoundariesCat | kBreakerCat; // 0011

boundariesCat.physicsBody.categoryBitMask = kBoundariesCat;
boundariesCat.physicsBody.collisionBitMask = kBoundariesCatCollisionMask;

breakerCat.physicsBody.categoryBitMask = kBreakerCat;
breakerCat.physicsBody.collisionBitMask = kBreakerCatCollisionMask;

breakerCat.physicsBody.categoryBitMask = blockCat;
breakerCat.physicsBody.collisionBitMask = kBlockCatCollisionMask;

此外,如果要启用联系人处理程序,请设置contactTestBitMasks:

static const uint32_t kBoundariesCatContactMask = kBreakerCat | kBlockCat;
static const uint32_t kBreakerCatContactMask    = kBoundariesCat | kBlockCat;
static const uint32_t kBlockCatContactMask      = kBoundariesCat | kBreakerCat;

boundariesCat.physicsBody.contactTestBitMask = kBoundariesCatContactMask;
breakerCat.physicsBody.contactTestBitMask = kBreakerCatContactMask;
breakerCat.physicsBody.contactTestBitMask = kBlockCatContactMask;