Is it possible to deactivate collisions in physics bodies in spriteKit?

前端 未结 4 1946
栀梦
栀梦 2020-12-09 19:12

I\'m looking at doing the best way to collect items with my hero in my spriteKit game for iOs, and after to try a few ways to do it, my conclusion is the best way would be t

4条回答
  •  臣服心动
    2020-12-09 19:25

    Check out collisionBitMask, categoryBitMask, and contactTestBitMask in the SKPhysicsBody class.

    Essentially, physics bodies with the same collisionBitMask value will "pass-through" each other.

    • Correction: If the category and collision bits match, they will interact. If they do not match, those two will not interact. And if the collision bits, and category bits, are both zero, of course that item will interact with nothing whatsoever.

    Then, you set the categoryBitMask and contactTestBitMask values to create an SKPhysicsContact Object on contact. Finally, your Class should adopt the SKPhysicsContactDelegate protocol. Use the - didBeginContact: method to detect and handle the SKPhysicsContact object.

    static const uint8_t heroCategory = 1;
    static const uint8_t foodCategory = 2;
    --
    food.physicsBody.categoryBitMask = foodCategory;
    food.physicsBody.contactTestBitMask = heroCategory;
    food.physicsBody.collisionBitMask = 0;
    --
    hero.physicsBody.categoryBitMask = heroCategory;
    hero.physicsBody.contactTestBitMask = foodCategory;
    hero.physicsBody.collisionBitMask = 0;
    --
    -(void)didBeginContact:(SKPhysicsContact *)contact {
        SKPhysicsBody *firstBody = contact.bodyA;
        SKPhysicsBody *secondBody = contact.bodyB;
    }
    

提交回复
热议问题