2

我正在制作一个由玩家、障碍物和硬币组成的相对简单的游戏。我的目标是仅在障碍物和玩家之间发生碰撞,并让玩家和硬币之间的接触触发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函数将硬币从视图中移除,并将一枚硬币添加到用户的货币银行中。此函数在玩家击中硬币时调用,但在硬币被移除之前发生碰撞。碰撞对游戏玩法非常具有破坏性,我一直无法找到可行的解决方案。有什么建议么?

4

1 回答 1

1

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

于 2016-01-25T02:34:36.693 回答