SpriteKit:如何防止两个 SKSpriteNode 碰撞(in Swift)
SpriteKit: How to prevent two SKSpriteNodes from colliding (in Swift)
我正在制作一个相对简单的游戏,由玩家、障碍物和硬币组成。我的目标是只在障碍物和玩家之间发生碰撞,并让玩家和硬币接触以触发 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,2,3 它们必须是 2 的幂; 0x1 << 0、0x1 << 1、0x1 << 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
函数从视图中删除硬币并将一枚硬币添加到用户的货币银行。当玩家击中硬币时调用此函数,但在硬币被移除之前发生碰撞。碰撞非常破坏游戏玩法,我一直无法找到可行的解决方案。有什么建议吗?
你的问题是你的类别,搜索位掩码的工作原理。你不能做 1,2,3 它们必须是 2 的幂; 0x1 << 0、0x1 << 1、0x1 << 2 等