Why my node calls didBeginContact method when its not in the Scene

核能气质少年 提交于 2019-12-11 20:07:15

问题


func didBeginContact(contact: SKPhysicsContact) {

        if contact.bodyA.categoryBitMask == PhysicsCategory.overPow || contact.bodyB.categoryBitMask == PhysicsCategory.overPow{
            let sk = delegateForCollision!.view as! SKView
            let newScene = GG(fileNamed: "GG")
            newScene!.delegateFor = delegateForCollision
            newScene?.scaleMode = .AspectFill
            sk.presentScene(newScene)
        }
        if contact.bodyA.categoryBitMask == PhysicsCategory.glem && contact.bodyB.categoryBitMask == PhysicsCategory.kappa{
            contact.bodyA.node?.removeFromParent()

            glem++
            glemLabel.text = "SKAŁY: \(glem)"

        }

        if contact.bodyB.categoryBitMask == PhysicsCategory.glem && contact.bodyA.categoryBitMask == PhysicsCategory.kappa{
            contact.bodyB.node?.removeFromParent()

            glem++
            glemLabel.text = "SKAŁY: \(glem)"
        }


    }

glem and kappa categoryBitMask nodes are colliding so glem variable should be ++ one time then node is removed from scene but it looks like this method is called more times during next frames. I see it in logs because I added print("\(glem)") in glem didSet. Why does it happen?


回答1:


I changed glem++ to if contact.bodyB.node?.parent != nil{glem++} and now it works as it should.



来源:https://stackoverflow.com/questions/34188183/why-my-node-calls-didbegincontact-method-when-its-not-in-the-scene

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!