我有一个游戏,其中掉落节点掉落并击中基数精灵节点,游戏逻辑从那里运行。 当我从头开始设置新游戏时,碰撞检测完全按照应有的方式工作。 当我使用 NSCoding 从以前的保存创建游戏时,会出现我的问题。 在这两种情况下(新游戏和从保存游戏继续),物理实体是相同的 - 动态,相同大小的身体,相同的接触TestBitMask,相同的类别BitMask。我已经测试了所有这些,所以我知道这是真的。 物理接触委托也设置为正确的对象。 然而,在从扑救继续的游戏中,联系人没有注册,我不知道为什么。 我唯一能想到但无法弄清楚的是设置为我的物理联系人代表的对象,并且是我想要碰撞检测的对象被加载/取消存档的父级,而无需我实际调用 decodeObjectForKey。
任何帮助将不胜感激
func initBaseNumberSpritePhysicsBody() {
baseNumberSprite.physicsBody = nil
baseNumberSprite.physicsBody = SKPhysicsBody(rectangleOfSize: baseNumberSprite.size)
baseNumberSprite.physicsBody!.categoryBitMask = baseNumberCategory
baseNumberSprite.physicsBody!.contactTestBitMask = fallingNodeCategory
baseNumberSprite.physicsBody!.collisionBitMask = 0
baseNumberSprite.physicsBody!.usesPreciseCollisionDetection = true
baseNumberSprite.physicsBody!.allowsRotation = false
}
func initPhysicsBodyForFallingNode(node: NumberNode) {
node.physicsBody = nil
node.physicsBody = SKPhysicsBody(rectangleOfSize: node.size)
node.physicsBody!.categoryBitMask = fallingNodeCategory
node.physicsBody!.contactTestBitMask = baseNumberCategory
node.physicsBody!.collisionBitMask = 0
node.physicsBody!.allowsRotation = false
node.physicsBody!.velocity = nodeVelocity
}
func didBeginContact(contact: SKPhysicsContact) {
if isContactBetween(fallingNodeCategory, and: baseNumberCategory, contact: contact) {
handleContactBetweenFallingNodeAndBaseNumber(contact)
} else {
print("nUNKNOWN CONTACT OCCUREDn")
}
updateInternalState()
checkGameOverCondition()
}
required init?(coder aDecoder: NSCoder) {
// gameZone = aDecoder.decodeObjectForKey(gameZoneKey) as! GameZone
super.init(coder: aDecoder)
gameZone = self.children[0] as! GameZone //Not decoded by itself but somehow decoded with the this GameScene Object (the "self" object here)
gameZone.delegate = self
self.physicsWorld.contactDelegate = gameZone
}
override func encodeWithCoder(aCoder: NSCoder) {
super.encodeWithCoder(aCoder)
aCoder.encodeObject(gameZone, forKey: gameZoneKey) //Gets coded
}
我能够在这里找出我自己的问题。 我缺乏的基本理解是SKNodes对自己的子节点进行编码。
// gameZone = aDecoder.decodeObjectForKey(gameZoneKey) as! GameZone
游戏区对象是一个子节点。 所以我两次对它以及其他关键对象进行编码,这导致了我的问题。 这个问题与物理世界联系委托或编码/解码无关。