1

我有一个游戏,其中下降节点下降,击中基本数字雪碧节点和游戏逻​​辑运行从那里。当我从头开始创建一款新游戏时,碰撞检测的工作原理应该如何。我使用NSCoding从以前的保存中创建游戏时出现问题。在这两种情况下(新游戏和继续保存游戏)物理主体是相同的 - 动态,相同大小的主体,相同的contactTestBitMask,相同的categoryBitMask。我测试了所有这些,所以我知道这是真的。物理接触委托也被设置为正确的对象。然而,在一场继续保存的游戏中,联系人没有注册,我无法弄清楚原因。我能想到的唯一的东西,但我无法弄清楚的是被设置为我的物理接触委托的对象,并且是我想要碰撞检测的对象的父对象被加载/取消存档,但实际上并没有为它调用decodeObjectForKey。iOS SpriteKit碰撞检测失败后解码保存

任何帮助,将不胜感激

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 OCCURED\n") 
     } 

     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 
    } 

回答

0

我能够在这里找出我自己的问题。我缺乏的基本理解是SKNodes编码自己的子节点。

//  gameZone = aDecoder.decodeObjectForKey(gameZoneKey) as! GameZone 

gameZone对象是一个子节点。所以我编码它以及其他关键对象两次,这是导致我的问题。这个问题与物理世界接触委托或编码/解码无关。