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