SpriteKit:如何防止两个SKSpriteNodes发生碰撞(在Swift中)

时间:2016-01-25 02:14:59

标签: ios swift sprite-kit skphysicscontact

我正在制作一个由玩家,障碍物和硬币组成的相对简单的游戏。我的目标是仅在障碍物和玩家之间发生碰撞,并使玩家和硬币之间的接触触发didBeginContact

这里有一些代码:

//declare categoryBitMasks
struct physicsCategories {
    static let playerCategory : UInt32 = 1
    static let coinCategory: UInt32 = 2
    static let obstacleCategory : UInt32 = 3 
}

//assign physics categories 
playerNode!.physicsBody?.categoryBitMask = physicsCategories.playerCategory
playerNode!.physicsBody?.collisionBitMask = physicsCategories.obstacleCategory
playerNode!.physicsBody?.contactTestBitMask = physicsCategories.coinCategory


obstacleNode.physicsBody?.categoryBitMask = physicsCategories.obstacleCategory
obstacleNode.physicsBody?.collisionBitMask = physicsCategories.playerCategory
obstacleNode.physicsBody?.contactTestBitMask = 0


coinNode.physicsBody?.categoryBitMask = physicsCategories.coinCategory
coinNode.physicsBody?.collisionBitMask = 0
coinNode.physicsBody?.contactTestBitMask = physicsCategories.playerCategory

didBeginContact功能会从视图中移除硬币,并将一个硬币添加到用户货币银行。当玩家击中硬币时,此功能会调用,但在移除硬币之前会发生碰撞。碰撞对游戏玩法非常具有破坏性,我一直无法找到有效的解决方案。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您的问题是您的类别,搜索位掩码的工作方式。你不能做1,2,3他们必须是2的权力; 0x1<< 0,0x1<< 1,0x1<< 2等