与三种不同精灵的精灵碰撞?

时间:2014-02-02 16:17:48

标签: objective-c cocoa collision-detection sprite-kit

我创造了三个精灵,在我的场景中充当墙壁。然后我有一个精灵和一个分数。我希望精灵只有在接触到地板时才将分数设置为0(三个精灵中的一个)。这就是我对联系人所拥有的。

- (void)didBeginContact:(SKPhysicsContact *)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 & shipCategory) != 0 &&
    (secondBody.categoryBitMask & obstacleCategory) != 0)
{
    score = 0;
    myLabel.text = [NSString stringWithFormat:@"%i", score];
}
}

这是categoryBitMask

static const uint32_t shipCategory =  0x1 << 1;
static const uint32_t obstacleCategory =  0x1 << 1;
static const uint32_t wallCategory = 0x1 << 1;

Theese是雪碧,地板和墙壁的代码

-(SKSpriteNode *)floorNode
{
floorNode = [SKSpriteNode spriteNodeWithImageNamed:@"rectangle.png"];
floorNode.position = CGPointMake(160,100);
floorNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:floorNode.size];
floorNode.physicsBody.categoryBitMask = obstacleCategory;
floorNode.physicsBody.contactTestBitMask = shipCategory;
fireNode.physicsBody.usesPreciseCollisionDetection = YES;
fireNode.physicsBody.collisionBitMask = 2;

floorNode.physicsBody.dynamic = NO;

floorNode.zPosition = 1.0;
return floorNode;
}

-(SKSpriteNode *)walldxNode
{
walldxNode = [SKSpriteNode spriteNodeWithImageNamed:@"wall.png"];
walldxNode.position = CGPointMake(30, 568);
walldxNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:walldxNode.size];
walldxNode.physicsBody.categoryBitMask = wallCategory;

walldxNode.physicsBody.dynamic = NO;

return walldxNode;
}

-(SKSpriteNode *)wallsxNode
{
wallsxNode = [SKSpriteNode spriteNodeWithImageNamed:@"wall.png"];
wallsxNode.position = CGPointMake(290, 568);
wallsxNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:wallsxNode.size];
wallsxNode.physicsBody.categoryBitMask = wallCategory;

wallsxNode.physicsBody.dynamic = NO;

return wallsxNode;
}

-(SKSpriteNode *)fireButtonNode
{
fireNode = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship.png"];
fireNode.position = CGPointMake(160,450);
fireNode.xScale = 0.32;
fireNode.yScale = 0.32;
fireNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius: fireNode.size.height/2];
fireNode.physicsBody.categoryBitMask = shipCategory;
fireNode.physicsBody.dynamic = YES;
fireNode.physicsBody.contactTestBitMask = obstacleCategory;
fireNode.physicsBody.collisionBitMask = 2;
fireNode.physicsBody.usesPreciseCollisionDetection = YES;

fireNode.name = @"fireButtonNode";//how the node is identified later
fireNode.zPosition = 2.0;
return fireNode;
}

问题是,当与其他两个墙碰撞时,精灵将得分设置为0,这两个墙有不同的categoryBitMask。我不知道该怎么办。

2 个答案:

答案 0 :(得分:0)

您的类别位掩码都具有相同的值。让它们有这样的不同:

static const uint32_t shipCategory =  0x1 << 1; // this equals 2
static const uint32_t obstacleCategory =  0x1 << 2; // this equals 4
static const uint32_t wallCategory = 0x1 << 3; // this equals 8

答案 1 :(得分:0)

巴塔利亚是正确的,但是现在你应该使用枚举而不是一些陈旧的static const。我在这样的代码中做了这个,然后针对示例代码提出了一个错误,因为它没有使用Apple当前的最佳实践:

// These constans are used to define the physics interactions between physics bodies in the scene.
typedef NS_OPTIONS(NSUInteger, RockBusterCollionsMask) {
    RBCmissileCategory =  1 << 0,
    RBCasteroidCategory =  1 << 1,
    RBCshipCategory =  1 << 2

};