0
votes

I Have the same problem as here: SpriteKit GameScene function won't add SKSpriteNode after being called by button.

I Didn't understand the answer. How do I create an outlet for skView ?

Thank you very much for any clarification you could bring me !

Here is my code:

In my GameScene.swift

   override func didMoveToView(view: SKView) {
        loadMyView()
   }

   func loadMyView(){
        currentScore = 0
        loadSpecialView()
   }

   func loadSpecialView(){
        Person = SKSpriteNode(imageNamed: "Person")
        Person.size = CGSize(width:40, height: 7)
        Person.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 120)
        Person.zRotation = 3.14/2
        Person.zPosition = 2.0


        self.addChild(Person)
   }

When the player dies, a Restart Button appears with the following code in my GameviewController.swift:

var currentGame: GameScene!

override func viewDidLoad() {
        super.viewDidLoad()
        currentGame = GameScene()

        if let scene = GameScene(fileNamed:"GameScene") {
            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = true
            skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .ResizeFill

            scene.viewController = self
            skView.presentScene(scene)

        }
}

@IBAction func restartGame(sender: UIButton) {
        outBuy.hidden = true
        outRestart.hidden = true
        currentGame.loadMyView()

    }

When I launch the simulator, all works well and the Person is added. When I press restart, the person doesn't appear. What happened ?

1
I also tried to replace currentGame = GameScene() with - user3578549
currentGame = GameScene(fileNamed:"GameScene") - user3578549
To no use :/ I have been trying to find a solution for hours... and I am sure it is really simple. But I can't find what goes wrong in my code. - user3578549

1 Answers

0
votes

I found the solution ! Finally !

I changed currentGame = GameScene() to currentGame=scene and moved it just above skView.presentScene(scene)