1

I have a subclass of SKLabelNode that's instanced three or four times, each with a name, and a destination:

//

import SpriteKit

class MenuButton: SKLabelNode {

    var goesTo = SKScene()

    override init() {
        super.init()
        print("test... did this get called? WTF? HOW/WHY/WHERE?")
    }

    convenience init(text: String, color: SKColor, destination: SKScene){
        self.init()
        self.text = text
        self.color = color

        goesTo = destination
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let nextScene = goesTo
       // .....?????????????????
       // HOW DO I GET THIS TO FIND ITS SCENE...
       // THEN ITS VIEW, THEN PRESENT this nextScene????

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

I'm at a loss as to how to get at the view I need to call the scene change on.

Confused
  • 6,048
  • 6
  • 34
  • 75

1 Answers1

2

Every SKNode has a scene property which returns the scene where the node lives.

And every SKScene as a view property, so

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let nextScene = goesTo

    self.scene?.view?.presentScene(nextScene)

}
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • THANK YOU!!! Much better than my approach to this problem ;) `let myPointlessView = SKView() myPointlessView.presentScene(nextScene)` – Confused Nov 09 '16 at 20:32