【问题标题】:Sprite Collision With Three Different Sprites?精灵与三个不同精灵的碰撞?
【发布时间】:2014-02-02 16:17:48
【问题描述】:

我已经创建了三个在我的场景中用作墙壁的精灵。然后我有一个精灵和一个分数。我希望精灵仅在接触地板(三个精灵之一)时才将分数设置为 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;

这些是精灵、地板和墙壁的代码

-(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;
}

问题是精灵在与其他两个具有不同 categoryBitMask 的墙碰撞时也会将分数设置为 0。我不知道该怎么办。

【问题讨论】:

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


    【解决方案1】:

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

    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
    

    【讨论】:

      【解决方案2】:

      Batalia 是正确的,但现在你应该使用枚举而不是一些老套的 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
      
      };
      

      【讨论】:

        猜你喜欢
        • 2017-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多