Search code examples
iosswiftsprite-kitscene

Swift And Spritekit sceneDidLoad being called more than one


I'm working with a simple game , I have MenuScene : the user can choose the type of the game and GamePlayScene .

First time user choose game type every thing is good , but the second time all methods called twice , and the third time methods called three time etc .

I try to use didMoveToView but still have the same problem.

should I delete the scene after finish the game ? if yes how?.

this is how I transition between scenes .

extension SKScene {
    func presentSceneWith(NameOfScene scene : String){
        if let scene = SKScene(fileNamed: scene){
            scene.scaleMode = .aspectFill
            self.view?.presentScene(scene)
        }
    }
}

I use it for present to GamePlayScene and back to MenuScene.


Solution

  • First : I'm using Firebase Database .

    So the problem is : I was using If Statement inside the observe .

    like :

    private func searchForOpponents(){
       let ref = Database.database().reference()
       ref.child("activePlayers").child("waiting").observe( DataEventType.value) { (snapshot) in
       // the problem when I put if statement like this
       if snapshot == something {
          presentToNextScene()
       }
    }
    

    ref.observe called more than one in the second so basically I was present to next scene more than one time .

    The solution : in my case I use ref.observeSingelEvent() and change the DataEventType to childChange .