Sprite Kit:已检测到联系但无法获取代码以用于两个特定节点之间的联系

时间:2015-05-27 21:16:56

标签: sprite-kit

我已经定义了一个联系人类别位掩码:

typedef NS_OPTIONS(uint32_t, ContactCategory) {
    ContactCategoryPenguin  = 1 << 0,
    ContactCategoryFish     = 1 << 1,
    ContactCategoryObstacle = 1 << 2,
    ContactCategoryBaddy    = 1 << 3
};

我为两个对象设置了物理实体:

// Penguin physics

-(void)setUpPhysicsBody {
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
    self.physicsBody.categoryBitMask = ContactCategoryPenguin;
    self.physicsBody.contactTestBitMask = ContactCategoryObstacle;
    ...
}

// Background physics

-(void)setUpBackground {
    SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
    background.physicsBody.categoryBitMask = ContactCategoryObstacle;
    background.physicsBody.contactTestBitMask = ContactCategoryPenguin;
    background.anchorPoint = CGPointZero;
    background.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    ...
    [self addChild:background];
}

我已将场景指定为<SKPhysicsContactDelegate>,检查联系方式如下:

-(void) didBeginContact:(SKPhysicsContact *)contact {
    NSLog(@"Contact!");
    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 == ContactCategoryPenguin && secondBody.categoryBitMask == ContactCategoryObstacle) {
        NSLog(@"Success if this gets called!");
    }
}

我的第一个日志被打印出来了。但是,第二个日志没有。所以我知道正在检测联系人,但最终的条件声明并没有按照我的想法进行。

Urgh!

我的预感是,我把物理机构弄错了。但这个问题对我来说并不清楚。我已经指定skView.showsPhysics = YES;,物理机构看起来对我来说。

任何关于为什么这不起作用的建议将不胜感激。

3 个答案:

答案 0 :(得分:1)

这是处理联系人的另一种更简单的方式:

-(void)didBeginContact:(SKPhysicsContact *)contact {
    uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

    if (collision == (ContactCategoryPenguin | ContactCategoryFish)) {
        NSLog(@"Penguin and fish made contact...");
    }
}

答案 1 :(得分:0)

尝试我用于自己的sprite-kit游戏的方法。它应该工作正常。

enum CollisionMask: UInt32 {
case monster = 1
case star = 2
case endPoint = 4
case waypoint = 8
}

    let firstBody: SKPhysicsBody  = contact.bodyA
    let secondBody: SKPhysicsBody = contact.bodyB
    var sprite: SKSpriteNode!
    if firstBody.categoryBitMask == CollisionMask.monster.rawValue && secondBody.categoryBitMask == CollisionMask.star.rawValue {
            starContact(secondBody.node!)
            currentStar += 1

    }

答案 2 :(得分:0)

新的一天。感觉不那么累。问题很明显:

// Background physics

-(void)setUpBackground {
    SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
    background.physicsBody.categoryBitMask = ContactCategoryObstacle;
    background.physicsBody.contactTestBitMask = ContactCategoryPenguin;
    background.anchorPoint = CGPointZero;
    background.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    ...
    [self addChild:background];
}

在创建背景之前,我正在设置背景物理主体的属性。正确的代码应该是:

// Background physics

-(void)setUpBackground {
    SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
    background.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    background.physicsBody.categoryBitMask = ContactCategoryObstacle;
    background.physicsBody.contactTestBitMask = ContactCategoryPenguin;
    background.anchorPoint = CGPointZero;
    ...
    [self addChild:background];
}