未检测到 spritekit 物理碰撞
Posted
技术标签:
【中文标题】未检测到 spritekit 物理碰撞【英文标题】:spritekit physics collision not be detected 【发布时间】:2017-02-04 22:34:03 【问题描述】:我有 4 个物理物体都可以很好地检测碰撞。但是,当它们相互碰撞时,有两个物理物体不会检测到。不过,它们确实会检测到它们何时与其他物理物体发生碰撞。我有所有它们的contacttestbitmasks,所以我不明白为什么会出现问题。这是一些代码: 这是我设置物理体的地方:
struct PhysicsCategory
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let player : UInt32 = 0b1
static let bounce : UInt32 = 0b10
static let blueball : UInt32 = 0b100
static let redball : UInt32 = 0b1000
static let coin : UInt32 = 0b10000
这是我用于设置玩家物理体的代码,它是问题物理体之一:
player.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: player.size.width-40, height: player.size.height-40))
player.physicsBody?.isDynamic = true
player.physicsBody?.categoryBitMask = PhysicsCategory.player
player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball
player.physicsBody?.contactTestBitMask = PhysicsCategory.redball
player.physicsBody?.collisionBitMask = PhysicsCategory.None
这是检测碰撞的函数:
func didBegin(_ contact: SKPhysicsContact)
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
firstBody = contact.bodyA
secondBody = contact.bodyB
else
firstBody = contact.bodyB
secondBody = contact.bodyA
if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.redball != 0))
RedballDidCollideWithPlayer(player: firstBody.node as! SKSpriteNode, redball: secondBody.node as! SKSpriteNode)
if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.blueball != 0))
BlueballDidCollideWithPlayer(player: firstBody.node as! SKSpriteNode, blueball: secondBody.node as! SKSpriteNode)
if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.redball != 0))
RedballDidCollideWithBounce(bounce: firstBody.node as! SKSpriteNode, redball: secondBody.node as! SKSpriteNode)
if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.blueball != 0))
BlueballDidCollideWithBounce(bounce: firstBody.node as! SKSpriteNode, blueball: secondBody.node as! SKSpriteNode)
if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.coin != 0))
coinDidCollideWithplayer(player: firstBody.node as! SKSpriteNode, coin: secondBody.node as! SKSpriteNode)
if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.coin != 0))
coinDidCollideWithplayer(player: firstBody.node as! SKSpriteNode, coin: secondBody.node as! SKSpriteNode)
这是我用于设置 blueball 的代码。这是另一个有问题的物理体:
let blueball = SKSpriteNode(imageNamed: "blueball")
blueball.position = enemyb.position
blueball.physicsBody = SKPhysicsBody(circleOfRadius: blueball.size.width/2)
blueball.physicsBody?.isDynamic = true
blueball.physicsBody?.categoryBitMask = PhysicsCategory.blueball
blueball.physicsBody?.contactTestBitMask = PhysicsCategory.player
blueball.physicsBody?.contactTestBitMask = PhysicsCategory.bounce
blueball.physicsBody?.collisionBitMask = PhysicsCategory.None
blueball.physicsBody?.usesPreciseCollisionDetection = true
addChild(blueball)
let actionMove = SKAction.move(to: player.position, duration: 2)
这里的任何想法都会有所帮助。几天来我一直试图找到问题,但没有运气。
【问题讨论】:
注释这行代码然后再试一次,` player.physicsBody?.isDynamic = true` @Mina 做到了。不会改变任何东西。 为节点、玩家和蓝球评论它,我认为问题是因为 isDynamic 属性。 【参考方案1】:设置多个类别时,您必须将这些值或组合在一起。你的代码
player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball
player.physicsBody?.contactTestBitMask = PhysicsCategory.redball
只需设置类别两次,第二次覆盖第一次。将其更改为:
player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball | PhysicsCategory.redball
【讨论】:
以上是关于未检测到 spritekit 物理碰撞的主要内容,如果未能解决你的问题,请参考以下文章